views:

111

answers:

4

Hi Intellects,

I'm using Apache POI for reading Excel. I've been asked to store the cells into MySQL 5.1. While I'm reading Excel cells, I've been thrown NullPointerException. This is my code snippet :

  int rows = sheet.getPhysicalNumberOfRows();

  for(int r = 1; r<rows; r++) {
            HSSFRow row = sheet.getRow(r);
            int cols = row.getLastCellNum();

            for(int c=0; c < cols; c++) {
                HSSFCell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);

                switch(cell.getColumnIndex())    {

                   case 0:
                       ....
                       ....

                   case 1:
                       ....
                       ....


                   case ...:
                       ....
                       ....

                }
            }
  }

  // Here comes the code to insert into DB

If anyone can identify where it arises, post asap. If I left some logic you need just ask me...

A: 

I guess cell may be null, but you should use debugger to find out the exception place.

Andrew Bezzub
I'm have already checked whether file exists and row counts, bro...
venJava
A: 

From the code given, either sheet, row or cell will be null. The exception stack trace itself should tell you exactly what line is causing the problem.

First thoughts are that you're getting the physical number of rows in the sheet but getRow() gets you a logical row. You may want to look into using getFirstRowNum() and getLastRowNum() which appear to return logical rows.

But that just my thoughts. More information is needed before I can say for sure.


To get a stack trace given your exception npe, use:

npe.printStackTrace();

or:

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
npe.printStackTrace(pw);
pw.flush();
sw.flush();
// sw.toString(); to get a string for putting into a text box or something else.
paxdiablo
I'm getting expected results upto the first line, starts with 'int rows'. But after that only I'm getting the NullPointerException..
venJava
If the "int rows" line throws the exception, then sheet is null.
Petar Minchev
@ven, how are you setting sheet?
paxdiablo
I said 'int rows' returns what it has to return... After that line of code only I've this Exception...
venJava
HSSFSheet sheet = wb.getSheetAt(0);This is how I get sheet...
venJava
@ven, you need to post the entire copy of the exception.
paxdiablo
I've caught the exception in the JOptionPane. It just shows "java.lang.NullPointerException'. I've caught it as 'npe'. I've tried getCause() and getMessage() but both returns just 'null'
venJava
@ven, see the update on how to get the full stack trace. You need to examine that to find out where the problem lies.
paxdiablo
A: 

Is it possible that your array offset is off by one (for the row loop)?

for(int r = 1; r<rows; r++) {
  HSSFRow row = sheet.getRow(r);

instead of starting at offset 1, start at 0. This makes more sense (and matches your column loop).

In this case the result of HSSFRow row = sheet.getRow(r); could be null (which incidentally you should check for).

Edit#1: For example:

  if (sheet == null) {
        return;
  }

  int rows = sheet.getPhysicalNumberOfRows();
  for(int r = 1; r<rows; r++) {
        HSSFRow row = sheet.getRow(r);
        if (row != null) {
             int cols = row.getLastCellNum();

             for(int c=0; c < cols; c++) {
                 HSSFCell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);

                 if (cell != null) {
                    switch(cell.getColumnIndex())    {
                         case 0:
                         case 1:
                         case ...:

                     }
                 }
             }
      }
 }

If you look at the JavaDoc this is important

public int getPhysicalNumberOfRows()  {
  return _rows.size();
}

Returns the number of physically defined rows (NOT the number of rows in the sheet)

Edit#2: For the sake of further debugging please place the following try/catch around your for loop.

try {
     // Your for loop code here
} catch (Exception x) {
     x.printStackTrace();
}

Please paste a comment to this answer or update your original answer with the result of this output.

Syntax
I've tried, but still not working...
venJava
I have added an example showing how you should ensure the returned objects are not null. Of more importance is that the Javadoc for the sheet object you are using indicates that just because getPhysicalNumberOfRows returns 10 rows does not mean there is 10 rows.
Syntax
I can't get off from this error, still bro...
venJava
A: 

I don't explain your NPE, but propose you an other way to iterate over workbook, have you tried this ?

for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
    final Sheet sheet = workbook.getSheetAt(i);
    final Iterator<Row> rows = sheet.rowIterator();
    if (rows.hasNext()) {
       final Row header = rows.next();
       ....
    }
}
Sylvain M