如果getCellType()包含在对null的检查中,为什么我会通过getCellType()获得此NullPointerException?

时间:2019-06-26 22:41:00

标签: groovy apache-poi xssf

我有一个Groovy脚本,旨在将Excel表导出到markdown文件。我遇到一个问题,其中getCellType()抛出空指针异常。

我已经尝试过在两个地方测试单元格,以查看它是否为空-既在单元格传递到调用getCellType()的方法之前,也包括在方法本身内。我一遍又一遍地查看了自己的逻辑,但似乎找不到我所缺少的东西。

我已经对其进行了测试,以确保该功能在发现空单元之前一直有效,并且似乎可以正常工作。它精确地遍历了表格,但最终却抛出了空指针异常。

注意:如果不清楚目的,第一个for循环将打印第一列,然后打印第二个for循环,打印第二列下面的所有其他列。

    #!/usr/bin/env groovy

    @Grab(group = 'org.apache.poi', module = 'poi', version = '4.1.0')
    @Grab(group = 'org.apache.poi', module = 'poi-ooxml', version = '4.1.0')

    import org.apache.poi.xssf.usermodel.XSSFWorkbook
    import org.apache.poi.xssf.usermodel.*
    import org.apache.poi.ss.usermodel.*

    Workbook wb = new XSSFWorkbook(new File(this.args[0]))

    DataFormatter formatter = new DataFormatter()
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator()

    PrintStream out = new PrintStream(new FileOutputStream(this.args[1]), true, "UTF-8")

    Sheet sheet = wb.getSheetAt(0)
    int lastRow = sheet.getLastRowNum()

    static private void handleCell(Cell cell, FormulaEvaluator evaluator, DataFormatter formatter, PrintStream out){

        if(cell != null){
            CellValue cellValue = evaluator.evaluate(cell);

            switch (cellValue.getCellType()) {
                case CellType.BOOLEAN:
                    out.print("Cell: " + cellValue.getBooleanValue() + " Type: Boolean")
                    break;
                case CellType.NUMERIC:
                    out.print("Cell: " + cellValue.getNumberValue() + " Type: Numeric")
                    break;
                case CellType.STRING:
                    out.print("Cell: " + cellValue.getStringValue() + " Type: String")
                    break;
                case CellType.BLANK:
                    out.print("Cell: BLANK Type: Blank")
                    break;
                case CellType.ERROR:

                    break;

                // CELL_TYPE_FORMULA will never happen
                case CellType.FORMULA:
                    break;

            }
        }
    }

    for ( int r = 0 ; r <= lastRow ; r++ ){
        cell = sheet.getRow(r).getCell(0)
        if(cell.getCellType() != CellType.BLANK){
            out.println(cell)
        }
    }
    for ( int r = 0 ; r <= lastRow ; r++ ) {
        boolean firstCell = true
        int lastCol = sheet.getRow(r).getLastCellNum()
        if ( r == 1){
            for ( int c = 1 ; c <= lastCol ; c++){
                if(firstCell){
                    out.print("| ")
                }
                out.print(" --- |")
                firstCell = false
            }
            out.println()
        }

        firstCell = true
        for( int c = 1 ; c <= lastCol ; c++ ){
            Cell cell = sheet.getRow(r).getCell(c)

            if(firstCell){
                out.print("| ")
            }
            if(cell != null){
                handleCell(cell, evaluator, formatter, out)
            }
            out.print(" | ")
            firstCell = false
        }
        out.println()
    }

错误消息:

Caught: java.lang.NullPointerException: Cannot invoke method getCellType() on null object
java.lang.NullPointerException: Cannot invoke method getCellType() on null object
    at org.apache.poi.ss.usermodel.CellValue$getCellType.call(Unknown Source)
    at excel2md.handleCell(excel2md.groovy:25)
    at excel2md.run(excel2md.groovy:82)

1 个答案:

答案 0 :(得分:0)

Axel Richtertim_yates均根据上述评论找到了问题。空白单元格返回一个空CellValue,该对象是引发空指针异常的对象。我将方法中的测试条件更改为if(cell.getCellType()!= CellType.BLANK)并解决了问题。