使用XWPFDocument打开docx文件时出现NullPointerException [英] NullPointerException when opening a docx file with XWPFDocument

查看:1468
本文介绍了使用XWPFDocument打开docx文件时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java打开Microsoft Word 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());  
        }  
    }
}

这是堆栈跟踪:

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

我在项目中包含了以下JAR(从poi-bin-4.1.2-20200217开始): 项目中的JAR列表

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

我在做什么错了?

推荐答案

我设法使用XWPFDocument类打开Word文档.这也回答了此处发布的问题: Apache POI XSSFWorkbook向文件抛出NullPointerException . 这是我的操作方式:

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. 我直接将JAR添加到了项目中-请参阅项目层次结构- Eclipse项目结构
  2. 我已将JAR添加到Java构建路径中-参见 Java构建路径
  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

我在Java 8和JDK11中都对此进行了测试,并且它们都可以工作.

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

现在打开文档.

这篇关于使用XWPFDocument打开docx文件时出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆