Apache POI .getCell() 错误,如果空白,java.lang.NullPointerException 无法读取单元格 [英] Apache POI .getCell() error, java.lang.NullPointerException Cannot Read Cell if blank

查看:44
本文介绍了Apache POI .getCell() 错误,如果空白,java.lang.NullPointerException 无法读取单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在使用 Apache POI 的 Excel 文件的 .getCell() 方法时遇到了问题.如果我尝试实现 Cell newCell = sheet.getRow(rowNumber).getCell(columnNumber) 或类似的东西,我总是得到错误

I have been having trouble recently with Apache POI's .getCell() method for Excel files. If I try to implement Cell newCell = sheet.getRow(rowNumber).getCell(columnNumber) or something similar, I always get the error

Exception in thread "main" java.lang.NullPointerException
at ExcelWriter.getWorkbook(ExcelWriter.java:80)
at Gui2.<init>(Gui2.java:93)
at Main.main(Main.java:24)

如果单元格为空,它指向我正在实现 .getCell() 方法的行.我有代码应该确定单元格是否为空,然后如果为真则打印一些内容,但似乎单元格为空白会导致程序出错.然而,这只发生在某些情况下,我发现如果我创建一个工作表并进行 for 循环以在每一行中创建一个新单元格,我可以读取一个空白单元格并确定它是空白的.但是,如果我随后在该工作表中的任何单元格中键入一个值,然后尝试读取不同的空单元格,我将再次收到错误消息.我不明白为什么只有当我尝试读取空单元格时才会出现此错误,但我需要在我的程序中循环遍历它们.这是我的代码片段以及导致错误的原因.

which points to the line I am implementing the .getCell() method in if the cell is null. I have code that is supposed to determine if the cell is null and then print something if that is true, but it seems as though the cell being blank causes an error with the program. This only happens in some cases however, and I have found that if I create a sheet and make a for loop to create a new cell in every row, I can read a blank cell and determine it is blank. However, if I then type a value into any cell in that sheet, and then try to read a different null cell, I get the error message again. I do not understand why I get this error only when I try to read null cells, but I need to loop through them in my program. Here is a snippet of my code and what is causing the error.

public void getWorkbook(String fileName)
{
    try 
    {
        existingFile = new FileInputStream(new File(fileName));
        workbook = new XSSFWorkbook(existingFile);
        Sheet newSheet = workbook.getSheet("Test3");
        //createSheet("Test3", workbook);

        Cell newCell = newSheet.getRow(1).getCell(1);
        if(newCell == null)
        {
            System.out.println("Null");
        }
        else if(newCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
        {
            System.out.println("Number");
        }
        //System.out.println(newCell.getStringCellValue());
    } 
    catch (FileNotFoundException e) //Couldn't find file
    {
        e.printStackTrace();
    } 
    catch (IOException e) //Couldn't create workbook
    {
        e.printStackTrace();
    }
}

为了清楚起见,代码中引用的单元格 B2 是空白的.但是,A1 不是,但是当我读取 A1 时,系统会按预期打印数字".

Just to be clear, the cell B2, which is referenced in the code, is blank. However, A1 is not, but when I read A1 the system prints "Number" as it is supposed to.

推荐答案

Apache POI JavadocsSheet.getRow(num) 可以返回 null,如果该行从未被使用过,因此没有使用't 已写入文件

As explained in the Apache POI Javadocs, Sheet.getRow(num) can return null, if the row has never been used and therefore hasn't been written to the file

Apache POI 文档中关于迭代行和单元格的介绍,如果你想遍历所有行,然后获取一个特定的列,你需要做这样的事情:

As covered in the Apache POI docs on iterating over rows and cells, if you want to loop over all rows, then fetch one specific column, you need to do something like:

// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());

for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
   Row r = sheet.getRow(rowNum);
   if (r == null) {
      // No entries in this row
      // Handle empty
      continue;
   }

   int cn = 1;
   Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
   if (c == null) {
      // The spreadsheet is empty in this cell
   } else {
      // Do something useful with the cell's contents
   }
}

这篇关于Apache POI .getCell() 错误,如果空白,java.lang.NullPointerException 无法读取单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆