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.(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.(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.(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.(SchemaTypeSystemImpl.java:1522)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:273)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.(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?
解决方案
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:
I added the JARs directly to the project- see the project hierarchy -Eclipse project structure
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.