0

I am getting a NullPointerException when opening a Microsoft Word docx file from Java. This is my code:

public class MainClass {
    public static void main(String[] args) {
        try {
            File file = new File("C:\\DevProjects\\Word\\test1.docx");
            FileInputStream fis = new FileInputStream(file);
            XWPFDocument document = new XWPFDocument(fis) ;
        }catch(Exception e) {  
            System.out.println(e.getMessage());  
        }  
    }
}

This is the stacktrace:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:156)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
    at java.lang.reflect.Field.get(Field.java:393)
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:775)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.<clinit>(Unknown Source)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:180)
    at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:184)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:145)
    at com.word.MainClass.main(MainClass.java:21)
Caused by: java.lang.RuntimeException: Could not instantiate SchemaTypeSystemImpl (java.lang.reflect.InvocationTargetException): is the version of xbean.jar correct?
    at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.loadTypeSystem(Unknown Source)
    at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.<clinit>(Unknown Source)
    ... 13 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    ... 15 more
Caused by: java.lang.NullPointerException
    at org.apache.xmlbeans.impl.schema.ClassLoaderResourceLoader.getResourceAsStream(ClassLoaderResourceLoader.java:33)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.getLoaderStream(SchemaTypeSystemImpl.java:2249)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.<init>(SchemaTypeSystemImpl.java:1522)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:273)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(SchemaTypeSystemImpl.java:185)
    ... 19 more

I've included the following JARs (from poi-bin-4.1.2-20200217) to my project: List of JARs in the project

What am I doing wrong?

Ephraim
  • 11
  • 1
  • Could you please also attach that document test1.docx ? – fpezzini May 08 '20 at 08:37
  • Also, when you're stepping through the code step by step and you arrive at that line of code that is throwing the exception, what variable is null that shouldn't? That could give you hints on what could be going wrong. – fpezzini May 08 '20 at 08:39
  • The line of code that throws the exception is " XWPFDocument document = new XWPFDocument(fis) ;". The Microsoft Word document is a one line document used for testing. It opens perfectly in Microsoft Word – Ephraim May 08 '20 at 09:16
  • I'd recommend you step into that constructor call when debugging this code to see which variable is null. Null pointer exception means a certain assumption is not valid in this case. It could be a case of malformed docx file, or perhaps file not found, maybe there's a weird character in there. – fpezzini May 09 '20 at 10:20

1 Answers1

0

I managed to open the Word document using the XWPFDocument class. This also answers the question posted here: Apache POI XSSFWorkbook throwing NullPointerException for file. Here is how I did it:

  1. I added the JARs directly to the project- see the project hierarchy -Eclipse project structure
  2. I added the JARs to the Java Build Path- see here Java Build Path

I tested this both in Java 8 and JDK11 and they both work.

Now the document opens.

Ephraim
  • 11
  • 1