繁体   English   中英

尽管在Apache POI Java中具有非NULL行,但同时获得excel Cell时却出现NullPointerException

[英]NullPointerException while getting excel Cell despite having non NULL Row in Apache POI Java

我正在使用Apache POI读写Excel文件。以下是生成NullpointerException的代码段:

   int rowcount = 0;
   while (rowcount < Header.MAX_ROW_COUNT) {
       try {
            gpnameRow = gpNameSheet.getRow(rowcount);
            } catch (Exception e) {              
            rowcount++;
            continue;
          }

        int index = 0;
        Cell indexCell = gpnameRow.getCell(0);
        index = (int) indexCell.getNumericCellValue();
        System.out.println(index);
        rowcount++; 
       } 

Cell indexCell = gpnameRow.getCell(0); 此行产生NullPointerException。

该错误的可能原因是什么?

请发布stacktrace,确定gpnameRow.getCell(0)异常,我认为应该是下一行,即indexCell.getNumericCellValue()您需要在调用该单元格的任何方法之前检查该单元格是否为null。

if( indexCell != null ){
        index = (int) indexCell.getNumericCellValue();
        System.out.println(index);
        rowcount++; 
}

浏览工作表时,可能会有一些空白的单元格,并且可能导致空指针异常。

请一次验证您的Excel。.在这里100%确保您没有得到行。 row is null在此处row is null ,因此您会遇到异常。如果其他原因让我们知道,希望我们可以解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM