0

The code below is supposed to make a copy of the excel file Pristine Copy (a .xlsx file). The name of the copy is copyTest. The copy is made, and when I open it manually, the excel file has the contents as expected. However, when i try and open the new file in the code, and write into it, it throws an error.

public static void main(String[] args)throws IOException, InvalidFormatException {

    String fileLocation = "C:\\Users\\someUser\\Documents\\Project Docs\\Pristine Copy.xlsx";
    String newFileLocation = "C:\\Users\\someUser\\Documents\\Project Docs\\copyTest.xlsx";

    Path source = Paths.get(fileLocation);
    Path dest = Paths.get(newFileLocation);
    Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);



    Workbook newWorkbook = WorkbookFactory.create(new File(newFileLocation));   
    System.out.println("There are "+newWorkbook.getNumberOfSheets()+" sheets.");

    MongoTest mt = new MongoTest(); 
    Document doc = mt.fetchRecord("HD123456", "SSE");
    Sheet sheet = newWorkbook.getSheet("SSE");
    writeToSheet(sheet, doc);

    FileOutputStream out = new FileOutputStream(newFileLocation);
    newWorkbook.write(out);
    out.close();
    System.out.println("Check");
    newWorkbook.close();

}

The stack trace is given below:

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.NullPointerException
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:147)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:225)
    at com.cerner.excelTest.ExcelFile.main(ExcelFile.java:63)
Caused by: java.lang.NullPointerException
    at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:211)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at org.apache.poi.util.DocumentHelper.readDocument(DocumentHelper.java:140)
    at org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:163)
    at org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument$Factory.parse(Unknown Source)
    at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:78)
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:145)
    ... 2 more

The problem occurs only when I try to open the file to write the workbook changes into it (the last few lines). The rest of the code works fine, without any issues. Any idea as to what the problem is? (I'm using version 3.17 of Apache POI)

user3377812
  • 33
  • 2
  • 8
  • 1
    The problem is that using `apache poi 3.17` it is **not** possible writing changes in the same `File` the `Workbook`was created from. See https://stackoverflow.com/questions/46146161/apache-poi-fileinputstream-works-file-object-fails-nullpointerexception. Maybe in `apache poi 4.0.0` this problem is solved. But I have not tried since I am **always** using `InputStream` to create a `Workbook`. – Axel Richter Oct 03 '18 at 05:57
  • thanks, that helped. but now, if i open the workbook using Pristine Copy, and write into copyTest, the changes are made to Pristine copy as well. – user3377812 Oct 03 '18 at 06:13
  • i'll stick to using InputStream to create a workbook for this. That works fine. Thanks again – user3377812 Oct 03 '18 at 06:22
  • Just tried. Problem is **not** solved in `apache poi 4.0.0`. – Axel Richter Oct 03 '18 at 06:47

0 Answers0