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

Java 使用Apache POI访问单元格时出现NullPointerException,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试使用ApachePOI读取Excel文件。 我的目标是从每一行获取单元格2和3,并将它们放入一个数组中。在某些行中,单元格3是空的(我不是说空、空或空)。如果在Excel中检查它们,则为空 其中一些空单元格(并非全部)导致线程“main”java.lang.NullPointerException中的NullPonterException:异常。当试图获取单元格值或单元格类型时会发生这种情况。我觉得那个地方没有牢房 同时如果我执行row.createCell(2).setCellTyp

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

其中一些空单元格(并非全部)导致线程“main”java.lang.NullPointerException中的NullPonterException:异常。当试图获取单元格值或单元格类型时会发生这种情况。我觉得那个地方没有牢房

同时如果我执行
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);          
    }
}
static ArrayList getFirstFile()引发IOException
{
FileInputStream fileIn=null;
ArrayList namesFirstFile=新的ArrayList();
字符串folderPath=“C:\\”;
字符串indivList1=“filename.xls”;
fileIn=newfileinputstream(folderPath+indivList1);
POIFSFileSystem fs=新的POIFSFileSystem(fileIn);
HSSF工作手册wb=新的HSSF工作手册(fs);
HSSFSheet sheet=wb.getSheetAt(0);
int rowNumber=6;//将前6行作为无关行跳过
HSSFRow行=sheet.getRow(行编号);
字符串firstName=“”;
字符串lastName=“”;
while(row.getCell(0.getCellType()==0){
lastName=row.getCell(1.toString();
firstName=row.getCell(2.toString();
namesFirstFile.add((rowNumber-6),(lastName+“”+firstName));
行数++;
row=sheet.getRow(rowNumber);
}
}

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

例如:

如果第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)仅当您为单元格设置值时。这里没有值。

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

例如:

如果第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)仅当您为单元格设置值时。这里没有值。

请尝试设置:

  • static Row.MissingCellPolicy创建为空

    将为丢失的单元格创建一个新的空白单元格

  • static Row.MissingCellPolicy返回为空

    缺少的单元格与空白单元格一样返回为null

  • static Row.MissingCellPolicy返回\u NULL\u和\u BLANK

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

对该主题进行了很好的讨论。

尝试设置:

  • static Row.MissingCellPolicy创建为空

    将为丢失的单元格创建一个新的空白单元格

  • static Row.MissingCellPolicy返回为空

    缺少的单元格与空白单元格一样返回为null

  • static Row.MissingCellPolicy返回\u NULL\u和\u BLANK

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


对该主题进行了很好的讨论。

已尝试!不起作用。结果和以前一样。我猜这是因为if检查值是否为null,而不是单元格是否存在。无论如何,感谢您的发布!那么您的行对象为空。你能显示你的代码和得到空指针异常的行吗。我可以帮你解决这个问题。toString();使用.getStringCellValue();不会改变任何事情。我正在使用.println验证所处理的内容。它和以前停在同一条线上。空指针位于firstName=row.getCell(2.toString()行;或firstName=row.getCell(2.getStringCellValue();不垫