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)