opening excel ooxml 2007 xlsx document returns nullPointer exception

 

Questions


How do I open a 2007 xlsx ooxml file with Apache POI? I’ve added everything to my class path but keep getting this java.lang.NullPointerException error:

"C:Program FilesJavajdk1.7.0_13binjava" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:1804,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.7.0_13jrelibcharsets.jar;C:Program FilesJavajdk1.7.0_13jrelibdeploy.jar;C:Program FilesJavajdk1.7.0_13jrelibjavaws.jar;C:Program FilesJavajdk1.7.0_13jrelibjce.jar;C:Program FilesJavajdk1.7.0_13jrelibjfr.jar;C:Program FilesJavajdk1.7.0_13jrelibjfxrt.jar;C:Program FilesJavajdk1.7.0_13jrelibjsse.jar;C:Program FilesJavajdk1.7.0_13jrelibmanagement-agent.jar;C:Program FilesJavajdk1.7.0_13jrelibplugin.jar;C:Program FilesJavajdk1.7.0_13jrelibresources.jar;C:Program FilesJavajdk1.7.0_13jrelibrt.jar;C:Program FilesJavajdk1.7.0_13jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.7.0_13jrelibextdnsns.jar;C:Program FilesJavajdk1.7.0_13jrelibextjaccess.jar;C:Program FilesJavajdk1.7.0_13jrelibextlocaledata.jar;C:Program FilesJavajdk1.7.0_13jrelibextsunec.jar;C:Program FilesJavajdk1.7.0_13jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.7.0_13jrelibextsunmscapi.jar;C:Program FilesJavajdk1.7.0_13jrelibextzipfs.jar;C:UsersPatrickDesktopxmlChangeroutproductionxmlChanger;C:poi-3.9poi-3.9-20121203.jar;C:poi-3.9poi-examples-3.9-20121203.jar;C:poi-3.9poi-excelant-3.9-20121203.jar;C:poi-3.9poi-ooxml-3.9-20121203.jar;C:poi-3.9poi-ooxml-schemas-3.9-20121203.jar;C:poi-3.9poi-scratchpad-3.9-20121203.jar;C:poi-3.9libcommons-codec-1.5.jar;C:poi-3.9libcommons-logging-1.1.jar;C:poi-3.9libjunit-3.8.1.jar;C:poi-3.9liblog4j-1.2.13.jar;C:poi-3.9ooxml-libdom4j-1.6.1.jar;C:poi-3.9ooxml-libstax-api-1.0.1.jar;C:poi-3.9ooxml-libxmlbeans-2.3.0.jar;C:Program Files (x86)JetBrainsIntelliJ IDEA 12.0.1libidea_rt.jar" Main
Connected to the target VM, address: '127.0.0.1:1804', transport: 'socket'
java.lang.NullPointerException
    at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:625)
    at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:91)
    at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:56)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73)
    at Main.main(Main.java:178)
Disconnected from the target VM, address: '127.0.0.1:1804', transport: 'socket'

Process finished with exit code 0

My code is as follows:

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        try {

            File file = new File(root_dir + "2013-03-13 iom diff.xlsx");
            Workbook workbook = WorkbookFactory.create(file); //fails here

            //...

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Again I have searched already for this but haven’t found any answers that worked (e.g. making sure the directory path is correct, going out of bounds etc.)

I asked a similar question before but the error was different because I believe I was using just the normal xlsx format and not the OOXML xlsx format when saving in Microsoft Excel.

Any ideas where this null pointer exception is coming from or a way of making the exception thrown more meaningful?

EDIT

When I use the following code to create a workbook, it opens with the code posted above:

        Workbook wb = new XSSFWorkbook();
        CreationHelper createHelper = wb.getCreationHelper();
        Sheet sheet = wb.createSheet("new sheet");


        Row r = sheet.createRow((short) 0);

        Cell cell = r.createCell(0);
        cell.setCellValue(1);


        r.createCell(1).setCellValue(1.2);
        r.createCell(2).setCellValue(createHelper.createRichTextString("This is a string"));
        r.createCell(3).setCellValue(true);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();

This implies an invalid format but I’m not sure what the deal is because I saved as OOXML in Excel

 

 

————————————————-

Answer

apache-poi,java,xlsx

Leave a Reply

Your email address will not be published. Required fields are marked *