java word转html 空指针,使用XWPFDocument打开docx文件时出现NullPointerException

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值