java - 当excel单元格中没有值时,显示错误

标签 java apache-poi

我使用 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/

相关文章:

java - 设置 JTable、Java 的条件

java - 在没有库的情况下读取 Java 中的 PKCS#1 或 SPKI 公钥

java - 如何使用 Java Apache POI 从 Excel 中删除整行?

java - 添加密码为 apache poi 的 word (.doc) 文件

java - Apache POI 出现错误,无法读取使用 FileInputStream 传递的文件

java - 在 Excel 中打开 xls 文件时使用 Apache POI 更新该文件

java - 使用 Java 的 Docker : path RUN javac

java - 如何在java中的url中发送加号运算符

java - AWS JAVA SDK错误 "The bucket is in this region: us-east-1. Please use this region to"

java - 如何使用 apache POI 和 PrimeFaces 将新工作表添加到现有 Excel 工作簿