I'm 开发者_StackOverflow中文版getting this exception while getting a cell. This is what I'm doing:
if(versionToAdd.equals("15.6")) {
Cell X = wb.getSheetAt(0).getRow(28+j).getCell((short)7);
Cell Y = wb.getSheetAt(0).getRow(28+j).getCell((short)8);
X.setCellValue(x);
Y.setCellValue(y);
}
this code it's inside of a for loop (j), and every time I iterate over a List of values, i get a new cell dynamically.
The problem is that, whenever it gets to the first col:row (H:29), i get a NullPointerException.
NOTE: The cells in the xls file are in blank... could that be the problem?
I got a collection of X/Y values in java, starts reading it, but whenever it reaches a certain amount of values, I get a NullPointerException in this line:
Cell cellx = row.getCell(8);
The java list is fine. If I put less X/Y pairs (i.e: 5 or 6 pairs), works great, but over a certain amount (let's say > 10), I get that exception.
Tried looking for the cells, everything seems to be fine
EDITED: Thanks people for your help! I started to use the XSSF/HSSF from POI. Tried to use it before, but I couldn't get it to run properly, but now I did and it works perfect by using the createRow-createCell methods :)
Both org.apache.poi.ss.usermodel.Sheet
and org.apache.poi.ss.usermodel.Row
are Iterable
. If you use the iterator in nested for-each
loops, as shown here, you should get only defined rows.
According to the POI documentation, if you ask for a row that is not defined, null is returned. The same is true for getting a cell from a row. I would assume that either a row or a cell does not exist.
Also, notice that the short version of getCell is deprecated in favor of getCell(int)
POI Sheet
POI Row
You can use getLastRowNum to verify that the number of rows that you are expecting exists. Same it true for getLastCellNum.
If you are creating a sheet / row / cell, you need to use the create methods. Not the get methods.
Create Row
Create Cell
The first three items on the user's guide are: how to create workbook, how to create sheet, how to create cells. You should check the documentation prior to posting questions.
POI Guide
精彩评论