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: [email protected]
        ReportedBy: [email protected]


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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to