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

查看:33
本文介绍了使用 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:

  • 静态 Row.MissingCellPolicy CREATE_NULL_AS_BLANK

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

A new, blank cell is created for missing cells.

  • 静态 Row.MissingCellPolicy RETURN_BLANK_AS_NULL

    缺失的单元格返回为空,空白单元格也是如此

    Missing cells are returned as null, as are blank cells

  • 静态 Row.MissingCellPolicy RETURN_NULL_AND_BLANK

    缺失单元格返回空值,空白单元格返回正常

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

  • 这个问题有话题的讨论.

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

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