HSSF Reading Blank Cell getting Null Pointer Exception

3.4k views Asked by At

I am working on the utility which dumps the excel sheet content to the database (postgres 9.2 in my case) , The application is working very smoothly when all the cells are filled but whenever i am trying to run my code on the excel sheet which is having empty cell it is giving me NULL POINTER EXCEPTION . Can any one help me......?

code.... snips ...

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

There are 1 answers

3
Scary Wombat On BEST ANSWER

Without knowing how you test for null on the cell, then if it throws a NPE on a certain line you should be about to test for null.

  if (c == null)

If this really doesn't work then of course you can always catch the NPE

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