views:

51

answers:

3

I'm using Apache POI 3.6. I've a column which is blank. I want it has to be read and then to the next cell. Even if I could resolve NullPointerException problem I could not get to the next cell.

Here's my code snippet :

HSSFCell cell = row.getCell(c);
String value = null;

switch (cell.getCellType()) {

    case HSSFCell.CELL_TYPE_FORMULA:
        value = "FORMULA value=" + cell.getCellFormula();
        break;

    case HSSFCell.CELL_TYPE_NUMERIC:
        value = "NUMERIC value=" + cell.getNumericCellValue();
        break;

    case HSSFCell.CELL_TYPE_STRING:
        value = "STRING value=" + cell.getStringCellValue();
        break;

    case HSSFCell.CELL_TYPE_BLANK:
        value="";
        break;

    case HSSFCell.CELL_TYPE_ERROR:
        value="error";
        break;

    default:
        break;
}

System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);

If any one can resolve it, help me...

A: 

You need to check if cell!=null, because if a cell doesn't exist in a row, row.getCell(c) returns null

ammoQ
As the 'cell' is an object, to get the value '==' or '!=' operator cannot be used, as far as I know. Isn't it? But the cell actually exists but it has a "Blank" value, i.e., no value.
venJava
You can use == or != to check for null values, which is what you should do. The cell most likely does not exist (in the data structure), which is why you have a NullPointerException problem.
ammoQ
A: 

Well, you could check for null before your switch statement, or you could change which call to row.getCell you make. Checking the Javadoc for POI there are 2 forms, the first is what you are using, the second has an additional parameter, of the type Row.MissingCellPolicy, where you can pass a value that would automagically transform null cells into blanks.

mezmo
Hi mezmo, Thanks a lot. I've tried with your suggestion and got it..
venJava
A: 

Hi Intellects,

I've finally got what I want. I thank mezmo for it. I want to share the exact code snippet to be changed. Just replace the line having :

HSSFCell cell = row.getCell(c);

with

HSSFCell cell=row.getCell(c, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK );
venJava