xHTMLImporter.convert(String,null)空指针异常

时间:2016-06-28 09:13:06

标签: html docx4j

我不明白为什么在尝试将HTML转换为docx时有时会出现空指针异常。

我使用带有所有依赖项的docx4j 3.2.1,这是代码

private void ajoutDescription(String description, int maxWidth) {
        if (description != null) {
            if (description.length() > 1) {
                description = formattageDescription(description, maxWidth);
                try {
                    xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
                    wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(description, null));
                } catch (Docx4JException e) {
                    System.out.println("bug ajout desc");
                    System.out.println(description);
                    e.printStackTrace();
                } 
                catch (NullPointerException e) {
                    System.out.println("bug ajout desc not Docx4j");
                    System.out.println(description);
                    e.printStackTrace();
                }
            }
        }
    }

这是xHTMLImporter.convert中的错误和描述arg(description,null)

<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta charset="ISO-8859-1" />
  <style type="text/css">p, ul, li, table, tr, td, font{font-family: Arial;font-size:120%}img{max-width:902px; height:auto;}</style>
 </head>
 <body>
  <p><img id="img1983" src="file:///C:/Users/david.guy/.caliberrm/4FA4F864-5D3E-4A30-A684-163FADFEB4D9.PNG" /></p>
 </body>
</html>
java.lang.NullPointerException
    at org.docx4j.finders.RangeFinder.apply(RangeFinder.java:59)
    at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:106)
    at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:109)
    at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:109)
    at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:109)
    at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:109)
    at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:109)
    at org.docx4j.TraversalUtil.<init>(TraversalUtil.java:209)
    at org.docx4j.convert.in.xhtml.BookmarkHelper.initBookmarkIdStart(BookmarkHelper.java:92)
    at org.docx4j.convert.in.xhtml.BookmarkHelper.getBookmarkId(BookmarkHelper.java:82)
    at org.docx4j.convert.in.xhtml.BookmarkHelper.anchorToBookmark(BookmarkHelper.java:187)
    at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:834)
    at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1236)
    at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1216)
    at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1216)
    at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:796)
    at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:680)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.ajoutDescription(ModuleToDocxSFDGenerator.java:1683)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.ajoutMaquettes(ModuleToDocxSFDGenerator.java:788)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.ecritureCURecursive(ModuleToDocxSFDGenerator.java:434)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.ecritureCURecursive(ModuleToDocxSFDGenerator.java:377)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.ecritureCU(ModuleToDocxSFDGenerator.java:345)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.lancerExportDocx(ModuleToDocxSFDGenerator.java:196)
    at com.simat.export.docx.ModuleToDocxSFDGenerator.docxParser(ModuleToDocxSFDGenerator.java:176)
    at com.simat.test.TestSFD.main(TestSFD.java:109)

我不知道如何解决这个问题。有些图片不会导致这个错误,但我不知道是哪些。

1 个答案:

答案 0 :(得分:0)

java.lang.NullPointerException
at org.docx4j.finders.RangeFinder.apply(RangeFinder.java:59)
at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:106)
:
at org.docx4j.finders.RangeFinder.walkJAXBElements(RangeFinder.java:109)
at org.docx4j.TraversalUtil.<init>(TraversalUtil.java:209)
at org.docx4j.convert.in.xhtml.BookmarkHelper.initBookmarkIdStart(BookmarkHelper.java:92)

删除@id后,您避免了错误,因为不再调用anchorToBookmark。

此问题看起来像是由wordMLPackage的内容触发,因此如果再次发生,请发布您的docx。