我使用 HSSFWORKBOOK
从 Excel 获取数据。当单元格中没有数据时,错误显示为:
Exception in thread "main" java.lang.NullPointerException at ExecuteTestcase.Testcase_execute.main(Testcase_execute.java:47)
我的代码是:
Sheet guru99Sheet = file.readExcel("D:\\SampleFramework.xls");
int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();
for(int i=1;i<rowCount+1;i++) {
Row row = guru99Sheet.getRow(i);
//Check if the first cell contain a value, if yes, That means it is the new testcase name
if(row.getCell(0)==null) {
//Print testcase detail on console
System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+ row.getCell(3).toString()+"----"+ row.getCell(4).toString());
//Call perform function to perform operation on UI
operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());
} else {
//Print the new testcase name when it started
System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");
}
}
请帮我解决这个问题。
最佳答案
您收到错误是因为您的 row.getCell() 之一返回 null 并且您没有设置 Row.MissingCellPolicy .
您可能需要 row.getCell(1, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK) 等。
关于java - 当excel单元格中没有值时,显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25505199/