Apache的POI .getCell()错误显示java.lang.NullPointerException无法读取细胞,如果空白 [英] Apache POI .getCell() error, java.lang.NullPointerException Cannot Read Cell if blank

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

问题描述

我一直有麻烦最近与Apache POI的.getCell为Excel文件()方法。如果我试图实现细胞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()方法就行了。我有code,它应该是确定是否细胞是空,然后打印东西,如果这是真的,但它好像在细胞是空白会导致程序错误。这仅在某些情况下发生然而,我发现,如果我创建了一个表,让一个for循环创建每一行中的新小区,我可以读一个空白单元格,并确定它是空白。但是,如果我再键入一个值,在该表的单元格,然后尝试读取不同的空单元格,我再次收到错误消息。我不明白为什么只有当我尝试读取空细胞我得到这个错误,但我需要遍历他们在我的计划。这里是我的code的片段,是什么原因造成的错误。

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单元格中,这是在code引用,是空白的。不过,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的Javadoc Sheet.getRow(NUM)可以返回空值,如果该行从未被使用,因此,还没有被写入到文件

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天全站免登陆