Bug 46548

Summary: HSSFPrintSetup throws NPE when accessing print settings of existing XLS
Product: POI Reporter: Dmitriy Kumshayev <dq>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 3.5-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: Unit test and proposed fix
XLS Example for the 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();
ps.getCopies();
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.

Thanks,
Dave
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 
src/testcases/org/apache/poi/hssf/data/46548.xls
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.