HSSF读空白单元获得空指针异常

时间:2013-12-25 05:53:22

标签: java hssf

我正在使用将excel表内容转储到数据库的实用程序(在我的情况下是postgres 9.2),当所有单元格都填满但是每当我尝试在excel上运行我的代码时,应用程序运行得非常顺利有空单元格的工作表它给我NULL指针异常。任何人都可以帮助我......?

代码......剪断......

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

1 个答案:

答案 0 :(得分:1)

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

  if (c == null)

如果这确实不起作用,那么你当然可以抓住NPE

  try {
      cellType = c.getCellType ();
  catch (NullPointerException e) {

       // oops Null
       // do something else.
  }