Bug 46548 - HSSFPrintSetup throws NPE when accessing print settings of existing XLS
Summary: HSSFPrintSetup throws NPE when accessing print settings of existing XLS
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Windows XP
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2009-01-16 06:24 UTC by Dmitriy Kumshayev
Modified: 2009-01-16 19:18 UTC (History)
0 users

Unit test and proposed fix (3.83 KB, patch)
2009-01-16 08:01 UTC, Dmitriy Kumshayev
Details | Diff
XLS Example for the bug (21.50 KB, application/octet-stream)
2009-01-16 08:06 UTC, Dmitriy Kumshayev

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitriy Kumshayev 2009-01-16 06:24:23 UTC
The following code throws NPE on certain XLS files

HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(xlsFile));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPrintSetup ps = sheet.getPrintSetup();
Comment 1 David Fisher 2009-01-16 07:28:42 UTC
Please attach an example of a file that fails. The smallest that demonstrates the trouble would be helpful. Without an example it is much, more difficult to solve the trouble.

Comment 2 Dmitriy Kumshayev 2009-01-16 08:01:52 UTC
Created attachment 23132 [details]
Unit test and proposed fix

When PLS record in Print Settings Block is long some xls files
contain additional Continue record going right after PLS.

The while loop in the constructor PageSettingsBlock() breaks 
right after the PLS record since it does no expect the Continue Record.

There is impression that there might be more than one 
PageSettingsBlock and the logic in org.apache.poi.hssf.model.Sheet
even creates and gets that second aggregated piece of records following PLS and Continue records, but finally just drops it. As a result 
PageSettingsBlock gets created without a very important PageSetupRecord
causing NPE when some of the getter methods (the ones which internaly supposed to deal with that missing PageSetupRecord) in  HSSFPrintSetup are called.

Someone put these comments at in Sheet.java

           // more than one 'Page Settings Block' at nesting level 1 ?
           // apparently this happens in about 15 test sample files
Comment 3 Dmitriy Kumshayev 2009-01-16 08:06:28 UTC
Created attachment 23133 [details]
XLS Example for the bug 

Please put this file to 
Comment 4 Josh Micich 2009-01-16 19:18:16 UTC
Fixed in svn r735179

Part of this change-list was the supplied patch (support for continued PLS records).  The rest was additional fixes for other ways of getting multiple PageSettingsBlocks.

I added the comments to Sheet.java in svn r683871 because I did not understand why sheets sometimes appeared to have more than one PageSettingsBlock.  Further investigation (of the test sample files) now, suggests that there are two situations where this occurs: with nested chart sub-streams and nested 'Custom View Settings' sub-streams each of which can contain its own PageSettingsBlock.  Prior to this fix POI handled PSBs in nested charts OK, but not within the 'Custom View Settings'.

There were also two circumstances in which POI would incorrectly create two PageSettingsBlocks where one was needed.  The first was in the case of a continued PLS record (fixed by the patch provided).  The other situation occurred when a WSBOOL record appears among the PSB records.  According to the OOO docs WSBOOL is not part of the PSB; it should occur before the PSB.  All but two of the junit test files agree with this.

The 'Custom View Settings' fix and the misplaced WSBOOL fix resulted in changes to the code near the comments you pointed out.