Bug 54607

Summary: NPE in XSSFSheet.getTopRow() when the top row is 1
Product: POI Reporter: Nikola Štohanzl <nikola.stohanzl>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: dominik.stadler
Priority: P2    
Version: 3.9-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: trivial test case, created in Excel 2010

Description Nikola Štohanzl 2013-02-25 08:45:08 UTC
Created attachment 29988 [details]
trivial test case, created in Excel 2010

Exception in thread "main" java.lang.NullPointerException
	at org.apache.poi.ss.util.CellReference.<init>(CellReference.java:88)
	at org.apache.poi.xssf.usermodel.XSSFSheet.getTopRow(XSSFSheet.java:1181)
	at poi.ReadTest.main(ReadTest.java:22)

for the second sheet (note the first visible row of first sheet is 2). File is created in Excel 2010, saving it in xls format (Excel 97-2003) solves the issue.

To reproduce:

Workbook wb = WorkbookFactory.create(new File("c:/0/xxx.xlsx"));
for (int si = 0; si < wb.getNumberOfSheets(); si++) {
	Sheet sh = wb.getSheetAt(si);
	System.out.println(sh.getSheetName() + "/" + sh.getTopRow());
}
Comment 1 Dominik Stadler 2013-05-30 08:44:45 UTC
It seems POI expects that sheets do have an attribute like this in sheetX.xml:
    topLeftCell="A2" 

However for your test-example, there is no such entry in sheet2.xml, seems the attribute is only there if a non-default top-row is set, but poi does not expect that. Other test-examples also don't have the attribute, seems a simple null check is missing.

BTW, the same applies for getLeftCol() for XSSF/SXSSF.
Comment 2 Dominik Stadler 2013-06-17 11:45:44 UTC
Fixed with SVN r1493686

        M       src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
        M       src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
        A       test-data/spreadsheet/54607.xlsx

Added a null-check as well as test-cases covering these changes.