You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2009/05/22 18:59:28 UTC

DO NOT REPLY [Bug 47244] New: NullPointerException with HSSFHeader

https://issues.apache.org/bugzilla/show_bug.cgi?id=47244

           Summary: NullPointerException with HSSFHeader
           Product: POI
           Version: 3.5-dev
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: PatchAvailable
          Severity: normal
          Priority: P2
         Component: HSSF
        AssignedTo: dev@poi.apache.org
        ReportedBy: jonathan.holloway@gmail.com


Apologies if this is a duplicate, HSSFHeader and HSSFFooter seem to throw
NullPointerException with some spreadsheets.

Exception caught
java.lang.NullPointerException
    at org.apache.poi.hssf.usermodel.HSSFFooter.<init>(HSSFFooter.java:44)
    at org.apache.poi.hssf.usermodel.HSSFSheet.getFooter(HSSFSheet.java:934)
    at
org.apache.poi.hssf.extractor.ExcelExtractor.getText(ExcelExtractor.java:386)

This happens when I'm using the org.apache.poi.hssf.extractor.ExcelExtractor in
particular.  FooterRecord/HeaderReccord appear to be null in these scenarios. 

BiffViewer fails with:

BiffViewer:
     [java] java.io.FileNotFoundException: no such entry: "Workbook"
     [java]     at
org.apache.poi.poifs.filesystem.DirectoryNode.getEntry(DirectoryNode.java:272)
     [java]     at
org.apache.poi.poifs.filesystem.DirectoryNode.createDocumentInputStream(DirectoryNode.java:124)
     [java]     at
org.apache.poi.poifs.filesystem.POIFSFileSystem.createDocumentInputStream(POIFSFileSystem.java:458)

If I open the original attachment in OpenOffice and save it this cures the
issue, so it's obviously some bad data in the spreadsheet.  I can't,
unfortunately send you the spreadsheet.

Attached is a patch that cures the issue, I'm not sure whether it is
appropriate to ignore the header/footer and return null, but it appears to cure
the issue and I get my spreadsheet output in the correct format.  Please let me
know if you need anything else.

Index: HSSFSheet.java
===================================================================
--- HSSFSheet.java    (revision 776494 ( https://svn.apache.org/viewcvs.cgi?view=rev&rev=776494 ))
+++ HSSFSheet.java    (working copy)
@@ -37,6 +37,8 @@
 import org.apache.poi.hssf.record.DVRecord;
 import org.apache.poi.hssf.record.EscherAggregate;
 import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.FooterRecord;
+import org.apache.poi.hssf.record.HeaderRecord;
 import org.apache.poi.hssf.record.NoteRecord;
 import org.apache.poi.hssf.record.Record;
 import org.apache.poi.hssf.record.RowRecord;
@@ -917,7 +919,11 @@
      * @return The Document header.
      */
     public HSSFHeader getHeader() {
-        return new HSSFHeader(_sheet.getPageSettings().getHeader());
+        HeaderRecord header = _sheet.getPageSettings().getHeader();
+        if (header != null) {
+            return new HSSFHeader(header);
+        }
+        return null;
     }

     /**
@@ -925,7 +931,11 @@
      * @return The Document footer.
      */
     public HSSFFooter getFooter() {
-        return new HSSFFooter(_sheet.getPageSettings().getFooter());
+        FooterRecord footer = _sheet.getPageSettings().getFooter();
+        if (footer != null) {
+            return new HSSFFooter(footer);
+        }
+        return null;
     }

     /**

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


DO NOT REPLY [Bug 47244] NullPointerException with HSSFHeader

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=47244


Jonathan Holloway <jo...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jonathan.holloway@gmail.com




-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


DO NOT REPLY [Bug 47244] NullPointerException with HSSFHeader

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=47244


Yegor Kozlov <ye...@dinom.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |NEEDINFO




--- Comment #1 from Yegor Kozlov <ye...@dinom.ru>  2009-05-30 05:05:11 PST ---
Can you attach a file that causes the problem? The suggested patch looks fine,
but I would like to have a clear test case before applying it. 

Regards,
Yegor

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


DO NOT REPLY [Bug 47244] NullPointerException with HSSFHeader

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=47244





--- Comment #2 from Josh Micich <jo...@gildedtree.com>  2009-05-31 01:04:19 PST ---
Sorry Jonathan and Yegor, I meant to reply earlier, but hadn't got round to it.

The proposed patch is NQR because it creates a header/footer object which is
detached from the HSSFSheet.  The newly created (empty) header / footer record
needs to be placed into the PageSettingsBlock, otherwise updating it will have
no effect.  That's easy enough, but it's messy to create objects that represent
their own absence.

I noticed that header/footer API on Sheet is a little deficient.  It's hard to
tell whether the usermodel objects are supposed to have a similar life-cycle as
the underlying biff records.  There's no methods on Sheet for update, delete or
doesExist, and I think it would clutter things to add these.  The getHeader /
getFooter methods seem to be called only twice from the full POI code-base.
ExcelExtractor assumes that getFooter() will return null when the footer is not
present.  HeadersAndFooters example expects that the footer will get lazily
created by HSSFSheet.getFooter(). This seems to suggest that HSSFHeader /
HSSFFooter should exist independent of HeaderRecord / FooterRecord.

I think a better solution might involve HSSFHeader / HSSFFooter directly
wrapping the PageSettingsBlock (which is lazily created, and already manages
the presence of all such contained records).  The HSSFHeader / HSSFFooter would
know how get / update / create and destroy the header / footer records, and
tell the PageSettingsBlock as needed.  This approach probably holds up better
in the XSSF world where headers and footers are further specialised to odd/even
etc (i.e. XSSFHeader should know about it's odd/even variants, not XSSFSheet).

We should also update the javadoc of getHeader / getFooter to say @return never
null.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


DO NOT REPLY [Bug 47244] NullPointerException with HSSFHeader

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=47244


Josh Micich <jo...@gildedtree.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEEDINFO                    |RESOLVED
         Resolution|                            |FIXED




--- Comment #3 from Josh Micich <jo...@gildedtree.com>  2009-06-03 20:43:51 PST ---
Fixed in svn r781645 ( https://svn.apache.org/viewcvs.cgi?view=rev&rev=781645 )

junits added

POI now tolerates missing header / footer records, and also adds them when
writing.

Also changed assumption (see bug 45777) that the header / footer text cannot
exceed 256 bytes.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org