从Excel读取数据。在行中获取空指针异常:object [i] [j] = row.getCell(j).toString();

时间:2018-08-12 06:46:43

标签: java apache-poi

/ ** 下面的代码是从excel读取数据,标题中提到的行中出现了异常

* /

public Object[][] getDataFromDataprovider() throws IOException{
        Object[][] object = null; 
        ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();

         //Read keyword sheet
         Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework");
       //Find number of rows in excel file
        int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();
        object = new Object[rowCount][5];       
        for (int i = 0; i < rowCount; i++) {
            //Loop over all the rows
            Row row = guru99Sheet.getRow(i+1);
            //Create a loop to print cell values in a row
            for (int j = 0; j < row.getLastCellNum(); j++) {
                //Print excel data in console
                object[i][j] = row.getCell(j).toString();
            }

        }
        System.out.println("");
          return object;     
    }

0 个答案:

没有答案