gpt4 book ai didi

java - 使用 Apache POI 访问单元格时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 10:00:45 25 4
gpt4 key购买 nike

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

其中一些空单元格(并非全部)导致 NullPonterException:线程“主”java.lang.NullPointerException 中的异常。它发生在尝试获取单元格值或单元格类型时。我的印象是那个位置没有细胞。

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

能否请您帮助我了解如何识别这些单元格(使用代码)并制定解决方法,以便我可以继续阅读直到文件结尾?

这是代码:

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);
}
}

最佳答案

您必须检查该单元格是否存在。请记住,如果存在一行并不意味着所有单元格都存在。如果该特定单元格不存在,则执行 get 操作会抛出空指针错误,但创建单元格不会抛出错误,因为它会在该特定行中创建该单元格。仅当该单元格对象存在时,您才可以对该单元格对象执行点运算符。

例如:

如果第 3 个位置的单元格存在,这将起作用:

row1.getCell(2).getStringCellValue();//如果值为字符串类型

否则您必须检查是否使用 if 并在需要时创建该单元格。

// This checks if the cell is present or it'll return null if the cell
// is not present (it won't check the value in cell)
if (row1.getCell(2) != null)
{
row1.createCell(2);
}

如果该单元格不存在,而您的唯一目标是获取该值,则可以在数组中设置一个默认值。

这两行行不通,因为它将单元格对象转换为字符串而不是它的内容:

lastName = row.getCell(1).toString();
firstName = row.getCell(2).toString();

您必须将其更改为:

lastName = row.getCell(1).getStringCellValue();
firstName = row.getCell(2).getStringCellValue();

当您在该单元格中没有任何值时,您不需要这样做:

if (row.getCell(2) != null)
{
row.createCell(2);
}
firstName = row.getCell(2).toString();

将此更改为:

if (row.getCell(2) != null)
{
firstName = row.getCell(2); // your other operations
} else {
firstname = "default value"; // your other operations
}

在创建单元格但未设置值时,无需获取单元格值。

只有在为单元格设置值时才必须使用 row.createCell(2);。在这里你没有任何值(value)。

关于java - 使用 Apache POI 访问单元格时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30895461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com