java - HSSF 读取空白单元出现空指针异常

标签 java hssf

我正在开发将 excel 工作表内容转储到数据库的实用程序(在我的例子中是 postgres 9.2),当所有单元格都被填充时,该应用程序运行得非常顺利,但每当我尝试在 excel 上运行我的代码时具有空单元格的工作表给了我 NULL POINTER EXCEPTION 。谁能帮我......?

代码....剪断...

public ArrayList fillList(int colIndex, int id, List<Cell> cells,
        String path) {
    // OrderedMap errorMap=new LinkedMap();
    String error = null;
    ArrayList<String> errorList = new ArrayList<String>();
    // errorList=null;
    try {
        FileInputStream fileIn = new FileInputStream(path);

        POIFSFileSystem fs;

        fs = new POIFSFileSystem(fileIn);

        HSSFWorkbook filename = new HSSFWorkbook(fs);
        Cell number = null;
        HSSFSheet sheet = filename.getSheetAt(0);
        Row firstRow = sheet.getRow(0);
        int flag = 0;

        String errorValue = null;

        int columnNo = colIndex;
        if (columnNo != -1) {
            for (Row row : sheet) {
                if (row.getRowNum() != 0) {
                    Cell c = row.getCell(columnNo);
                   // row.getCell(arg0, arg1)
                    // cells.add(c);
                    System.out.println(c.getCellType());
                    if (c.getCellType() == Cell.CELL_TYPE_STRING && 
                             (id == 2 || id == 3)) {
                        cells.add(c);
                    } else if (c.getCellType() == Cell.CELL_TYPE_NUMERIC
                            && id == 1) {
                        String s = row.getCell(columnNo).toString();
                        double d = Double.parseDouble(s);
                        String mob = Double.toString(d);
                        Cell sc = row.createCell((short) 2);
                        String text = NumberToTextConverter.toText(c
                                .getNumericCellValue());
                        // System.out.println(text);
                        sc.setCellValue(text);
                        cells.add(sc);
                        // Date date=c.getDateCellValue();

                    } else if (c.getCellType() == Cell.CELL_TYPE_NUMERIC && id == 4) {
                        String s = row.getCell(columnNo).toString();
                        double d = HSSFDateUtil.getExcelDate(c
                                .getDateCellValue());
                        // String date = Double.toString(d);
                        Cell sc = row.createCell((short) 2);
                        String date = new SimpleDateFormat("dd-MM-yyyy")
                                .format(c.getDateCellValue());
                        // System.out.println(text);
                        sc.setCellValue(date);
                        cells.add(sc);

                    } 
                    else if (c.getCellType() == Cell.CELL_TYPE_BLANK && id == 1   ) {
                        String s = row.getCell(columnNo).toString();
                        Cell sc = row.createCell((short)2);
                        sc.setCellValue("-");
                        cells.add(sc);

                    }
                    else {
                        switch (c.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            errorValue = Double.toString(c
                                    .getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            errorValue = c.getStringCellValue();
                            break;
                        }

                        errorList.add(c.getRowIndex() + "$" + columnNo
                                + "$" + errorValue + "$" + id);
                    }
                    /*
                     * if (c == null || c.getCellType() ==
                     * Cell.CELL_TYPE_BLANK) { cells.add(c); } else {
                     * 
                     * cells.add(c);
                     * 
                     * }
                     */

                    flag = 1;
                }// if to skip 1st row
            }
        } else {
            // System.out.println("could not find column " + columnWanted +
            // " in first row of " + fileIn.toString());
        }
        return errorList;
    } catch (IOException e) {

        e.printStackTrace();
    } finally {
    }
    return errorList;
}

最佳答案

如果不知道如何在单元格上测试 null,那么如果它在某一行抛出 NPE,您应该测试 null。

  if (c == null)

如果这确实不起作用,那么你当然可以随时 catch NPE

  try {
      cellType = c.getCellType();
  } catch (NullPointerException e) {
       // oops Null
       // do something else.
  }

关于java - HSSF 读取空白单元出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769493/

相关文章:

java - 如何将 XLS2CSVmra 限制为仅限一张纸?

java - 如何获取给定单元格的 (Java Apache POI HSSF) 背景颜色?

java - 使用 Apache POI 获取行数

java - 我们如何在java中将xml文件直接插入到mysql表中?

java - 没有找到适合 write(String) 的方法

java - Apache POI-HSSF 在将图片添加到 Excel 单元格时扭曲图像大小

java - 即使在类路径中包含 jar 文件后也无法在运行时找到类

java - 这个 while 循环有什么问题吗?

java - ServerSocket 连接问题

java - MongoDb Java 驱动程序 v3.x 和聚合框架