有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ApachePOI在缺少单元格时停止解析excel文件,无论缺少单元格策略如何

我正在编写一个脚本,对excel工作表中的每一行进行分析,并打印出该行中每个单元格的内容。以下是代码:

    for(int cntr=1; cntr<=firstSheet.getPhysicalNumberOfRows(); cntr++){
        Row currentRow = firstSheet.getRow(cntr);
        System.out.println("-------------Working on Row Number: "+currentRow.getRowNum());
        for(int cntr2 = 0; cntr2<currentRow.getLastCellNum(); cntr2++){
            Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
            if(currentCell==null){
                //cell doesn't have anything in there
                System.out.println("Cell in Row number "+(currentRow.getRowNum()+1)+" is null.");
            }
            switch(currentCell.getCellType()){
                case Cell.CELL_TYPE_STRING:
                    System.out.println("Current Cell: "+currentCell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.println("Current Cell: "+currentCell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.println("Current Cell: "+currentCell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_BLANK:
                    System.out.println("Cell in row number "+(currentRow.getRowNum()+1)+" is returned as blank");
                    break;
            }
        }

    }

问题是,在我的excel电子表格的第11行(如果从0开始计算,则为第10行)中有一个单元格,准确地说是3个单元格,其中没有任何内容。每次我的脚本达到这一点时,它都会抛出一个空指针异常并停止运行,尽管我的IDE表示构建成功。我对如何处理这个问题做了一些研究,发现了我在生产线上使用的MissingCellPolicy

    Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);

但我仍然会遇到NullPointerException错误。我的算法有什么问题

此外,以下是输出:

    Exception in thread "main" java.lang.NullPointerException

    ...

    -------------Working on Row Number: 10
    Current Cell: OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O
    Current Cell: NCGC00159408-02
    Current Cell: DL-Glucose
    Current Cell: 180.16
    Current Cell: -3.51
    Current Cell: 6.0
    Current Cell: 5.0
    -------------Working on Row Number: 11
    Current Cell: OC[C@@H](O)C(O)[C@@H](O)CO
    Current Cell: NCGC00165982-01
    Cell in Row number 11 is null.
   at excellibrarycreation.ExcelFileProcesser.processFile(ExcelFileProcesser.java:64)
   at excellibrarycreation.ExcelLibraryCreation.main(ExcelLibraryCreation.java:25)

Java结果:1 构建成功(总时间:11秒)


共 (3) 个答案

  1. # 1 楼答案

    你的问题在于if语句

    Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
    if(currentCell==null){
        ...
    }
    switch(currentCell.getCellType()){
                   .
                   .
                   .
    }
    

    不管currentCell是否为null,都要将其放入switch语句中。这将导致swtich语句在调用currentCell.getCellType()时抛出NullPointerException,如果currentCellnull。要消除这种情况,请将switch语句放入else子句中,如下所示:

    Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
    if(currentCell==null){
        ...
    } else {
        switch(currentCell.getCellType()){
                       .
                       .
                       .
        }
    }
    
  2. # 2 楼答案

    请执行两个更改,然后看,我认为它应该可以工作:

    第一次从for循环中删除“=”

    for(int cntr=1; cntr<firstSheet.getPhysicalNumberOfRows(); cntr++){
    

    第二次使用,如果它不改变你的结果/要求

    Row.CREATE_NULL_AS_BLANK       
    
  3. # 3 楼答案

    在代码中修改这个

        if(currentCell==null){
                    //cell doesn't have anything in there
          System.out.println("Cell in Row number "+(currentRow.getRowNum()+1)+" is null.");
          continue;// continue if row is null..meaning do not go to switch case statement
                        }
    
                }
                switch(currentCell.getCellType()){
             ////you do not want to enter here if currentCell is null
                    //cell doesn't have anything in there}
    

    希望这能解决你的问题