Bug 48598

Summary: code migration problem
Product: POI Reporter: satish <satishkk>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED INVALID    
Severity: normal CC: satishkk
Priority: P2    
Version: 3.5-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description satish 2010-01-22 01:30:42 UTC
We are using poi3.0.2.jar file.

where the code(attached) works fine for xls files

wb=WorkbookFactory.create(fin);
Sheet sheet = wb.getSheetAt(0);
Row row;
Cell cell;
for(int r = 0; r < rows; r++) {
	System.out.println();
	row = sheet.getRow(r);
	if(row != null) {
	for(int c = -1; c < cols; c++) {
	cell = row.getCell((short)c);
	}

where as if I  pass xlsx files its giving followig error.

java.lang.IllegalArgumentException: Cell index must be >= 0
	at org.apache.poi.xssf.usermodel.XSSFRow.getCell(XSSFRow.java:191)
	at org.apache.poi.xssf.usermodel.XSSFRow.getCell(XSSFRow.java:178)
	at org.apache.poi.xssf.usermodel.XSSFRow.getCell(XSSFRow.java:35)
	at ReadExcel.readBoth(ReadExcel.java:190)
	at ReadExcel.main(ReadExcel.java:39)

The problem is with row.getCell(-1)
this method returns null in POI3.0.2
the same method throws IllegalArgumentException in POI3.5

So we need to change the code?
Comment 1 Nick Burch 2010-01-22 02:23:04 UTC
row.getCell(-1) is not a valid thing to ask for

As the error message tells you, cells start at 0. Either ask for cells from 0 onwards, or use an iterator.