Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在excel行中循环会给我一个ArrayIndex…异常_Java_Excel_Loops_Selenium_Selenium Webdriver - Fatal编程技术网

Java 在excel行中循环会给我一个ArrayIndex…异常

Java 在excel行中循环会给我一个ArrayIndex…异常,java,excel,loops,selenium,selenium-webdriver,Java,Excel,Loops,Selenium,Selenium Webdriver,好吧,我被困住了,我相信这是一个简单的解决方案。基本上,我正在开始我的初始转换,或者至少构建一个演示,以用于selenium测试的关键字驱动框架。每行将包含用于驱动测试的数据。行完成后,下一行将包含下一个测试,依此类推。所以我刚开始,我有点麻烦。这是我的密码: public List<String> getRowData(String row){ Sheet sheet = null; List<String> getContents = new Arra

好吧,我被困住了,我相信这是一个简单的解决方案。基本上,我正在开始我的初始转换,或者至少构建一个演示,以用于selenium测试的关键字驱动框架。每行将包含用于驱动测试的数据。行完成后,下一行将包含下一个测试,依此类推。所以我刚开始,我有点麻烦。这是我的密码:

public List<String> getRowData(String row){
    Sheet sheet = null;
    List<String> getContents = new ArrayList<>();
    try{
        sheet = getWorkBook().getSheet("test");
        for(int i=0; i<sheet.getRow(i).length; i++){
            getRowData = sheet.getRow(i);
            for(Cell rowData : getRowData){
                System.out.print(String.format("Row info: %s\n", rowData.getContents()));
                getContents.add(rowData.getContents());
            }
        }
    }catch (BiffException e) {
        e.printStackTrace();
    } catch (IOException e) {
        System.out.print("This is an exception!");
    } catch (NullPointerException np){
        System.out.print("File does not exist!");
    }return getContents;
}

但它仍然进行了评估,并提供了相同的异常。我被困住了,希望你能帮助我在没有内容的情况下打破这个循环。谢谢您

更改整数i=0;我在那里添加了空指针,但不应该是空指针。我将添加一个ArrayIndexOutOfBounds Catch谢谢你xgeorgekx。出于测试目的,不管最后一行有数据,这都是测试集中的最后一个测试用例是的,但是如果中间有一个空行呢?啊,我明白了-在这种情况下,你会如何处理?比如说,如果真的没有剩下信息行了吗?您有足够的行数作为for循环的停止条件。所以,只要换掉破碎;使用continue;。这样,您将检查每一行,直到文件结束。如果为空,则跳到下一步等。
if(sheet.getRow(i) == null){
                break;
            }
for(int i = 0; i < sheet.getRows(); i++){
    if(sheet.getRow(i) == null) break;

    //rest of the code
}