Search code examples
office365docxdocx4j

docx4j - NullPointerException in VariablePrepare.prepare()


When I create a .docx using the online version of Office365, then my program crashes on the call to VariablePrepare.prepare()

Relevant code lines:

InputStream inputStream = new ByteArrayInputStream(docxFileContent);
WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(inputStream);
VariablePrepare.prepare(wordprocessingMLPackage);

Stack trace:

Caused by: java.lang.NullPointerException
    at org.docx4j.openpackaging.packages.WordprocessingMLPackage.transform(WordprocessingMLPackage.java:271)
    at org.docx4j.openpackaging.packages.WordprocessingMLPackage.filter(WordprocessingMLPackage.java:297)
    at org.docx4j.model.datastorage.migration.VariablePrepare.prepare(VariablePrepare.java:79)

My document created using Online Office 365: https://drive.google.com/file/d/1MGZpsKZokmw6nyLbt_aAxsueUBl5tMnq/view?usp=sharing

Note that when you inspect the properties of this file, it is missing values for basic stuff like Pages, Word Count, Character Count, etc. See https://drive.google.com/file/d/1Vvuf2zJvGqigX6SMVu-C6EMsYXu0378I/view?usp=sharing . If you open the document with a desktop version of office, then those values are suddenly populated and the crash no longer occurs. This issue is specific to documents sourced from Online Office 365.


Solution

  • Now fixed at https://github.com/plutext/docx4j/commit/b877520c0a02804f00b2fce57d56405f045947f0

    that bit of the code was assuming word/document.xml, which is incorrect for your document.

    This fix will be in docx4j 6.1.0 (and in a nightly release sooner).