使用Apache POI访问单元格时出现NullPointerException [英] NullPointerException when accessing a cell with Apache POI

查看:1488
本文介绍了使用Apache POI访问单元格时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache POI读取Excel文件。
我的目标是让细胞2&每行3个并将它们放入一个数组中。在某些行中,单元格编号3为空(我不是在谈论EMPTY,NULL或BLANK)。如果你在Excel中检查它们就空了。

I am trying to read an Excel file with Apache POI. My goal is to get the cells 2 & 3 from every row and place them into an array. In some rows cell number 3 is empty (I am not talking about EMPTY, NULL or BLANK). Just empty if you check them in Excel.

其中一些空单元格(不是全部)导致NullPonterException:线程main中的异常java.lang.NullPointerException 。它在尝试获取单元格值或单元格类型时发生。我的印象是该位置没有单元格。

Some of those empty cells (not all) cause the NullPonterException: Exception in thread "main" java.lang.NullPointerException. It happens when trying to get the cell value or the cell type. I have the impression that there is no cell at that location.

同时如果我执行 row.createCell(2).setCellType( 1); 对于该特定行,程序继续直到此类型的下一个单元格。这是继续从下一行读取单元格的唯一解决方法(我可以找到)。

At the same time if I do row.createCell(2).setCellType(1); for that specific row the program continues until next cell of this type. That is the only workaround (that I could find) to continue reading cells from next rows.

请你帮我理解如何识别这些单元格(带代码)和我的解决方法到位,所以我可以继续阅读直到文件的结尾?

Could you please help me understand how to identify these cells (with code) and put my workaround in place so I can continue reading till the end of the file?

这是代码:

static ArrayList<String> getFirstFile() throws IOException
{
    FileInputStream fileIn = null;

    ArrayList <String> namesFirstFile = new ArrayList <String>();
    String folderPath = "C:\\";
    String indivList1 = "filename.xls";

    fileIn = new FileInputStream(folderPath+indivList1);
    POIFSFileSystem fs = new POIFSFileSystem(fileIn);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    int rowNumber = 6; //skipping the first 6 rows as irrelevant
    HSSFRow row = sheet.getRow(rowNumber);
    String firstName = "";
    String lastName = "";

    while(row.getCell(0).getCellType() == 0) {
        lastName = row.getCell(1).toString();
        firstName = row.getCell(2).toString();

        namesFirstFile.add((rowNumber-6), (lastName + " " + firstName));
        rowNumber++;
        row = sheet.getRow(rowNumber);          
    }
}


推荐答案

尝试设置 Row.MissingCellPolicy


  • static Row.MissingCellPolicy CREATE_NULL_AS_BLANK


为缺失的单元格创建一个新的空白单元格。

A new, blank cell is created for missing cells.


  • static Row.MissingCellPolicy RETURN_BLANK_AS_NULL


    缺少的单元格返回null,如是空白单元格

    Missing cells are returned as null, as are blank cells


  • static Row.MissingCellPolicy RETURN_NULL_AND_BLANK


    缺少的单元格返回null,空白单元格正常返回

    Missing cells are returned as null, Blank cells are returned as normal


  • 这个问题对该主题进行了很好的讨论。

    This question has a good discussion on the topic.

    这篇关于使用Apache POI访问单元格时出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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