Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointer Exception while using Apache POI

 
Paras Ahuja
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if(cell8[j].getCellType()==Cell.CELL_TYPE_BLANK) throws a NullPointerException. Dont know why...
Please help.....

 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating an array with 8 elements in it, but you are not actually putting anything into the array, so you currently have 8 null array elements.

missed the lines ahead where you fill the array in.

but you are still getting a null pointer exception because there is nothing in the element.
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And fabs[j] = new String(); should be deleted. It does nothing. You might do well to get rid of the assignment to null in line 13; that is another problem waiting to happen later on.
 
Paras Ahuja
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you see this is these rows are 8th in list in the template i'm reading. I have same code for row1,row2,......,row7 before this one. But when i try to upload data from this excel sheet into my GUI, the error occurs at row8 (as i specified before). I am filling only row1 cells to be read and leaving all the remaining cells blank.
Why the error is not generated for row2,row3,.....row7?
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you post a sceen shot, as I am not following that link, sorry
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
row8[j].getCell(3) will return null if the cell is not defined (whatever that means). There are two quick fixes:
1) Check if row8[j] != null before using it.
2) Replace row8[j].getCell(3) with row8[j].getCell(3, Row.CREATE_NULL_AS_BLANK).
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has occurred because cell8[j] is null. You will need to debug to find out why.

On a side note, get into the habit of using braces for single line if conditions. The no braces style is horrible and prone to creating bugs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic