We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

NullPointerException when opening word document

Using Aspose.words for Java 14.4.1, some Word (docx) documents throw a NullPointerException in the following code:

public void convert(final InputStream inputStream, final OutputStream outputStream) throws ConversionException {
    try {
        final BufferedInputStream in = new BufferedInputStream(inputStream);
        final DocumentStreamConverter converter = converterFactory.getConverter(in);

        converter.convert(in, outputStream, options);
    } catch (final Exception e) {
        throw new ConversionException(e);
    }
}

One file causing this error is attached to this post.

Stack trace:

se.sjv.commons.fileconversion.ConversionException: se.sjv.commons.fileconversion.ConversionException: java.lang.NullPointerException
at se.sjv.commons.fileconversion.DocumentConversionManager.convert(DocumentConversionManager.java:65)
at se.sjv.commons.fileconversion.DocumentConversionManagerTest.convertIntoStreamableDocument(DocumentConversionManagerTest.java:69)
at se.sjv.commons.fileconversion.DocumentConversionManagerTest.convertKnownAsposeBugCases(DocumentConversionManagerTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: se.sjv.commons.fileconversion.ConversionException: java.lang.NullPointerException
at se.sjv.commons.fileconversion.converter.pdfa.DocumentConverter.performConversion(DocumentConverter.java:57)
at se.sjv.commons.fileconversion.converter.BaseConverter.convert(BaseConverter.java:42)
at se.sjv.commons.fileconversion.DocumentConversionManager.convert(DocumentConversionManager.java:63)
... 26 more
Caused by: java.lang.NullPointerException
at com.aspose.words.zz2E.zzZMT(Unknown Source)
at com.aspose.words.zz2E.zzZMY(Unknown Source)
at com.aspose.words.zz2F.zzB(Unknown Source)
at com.aspose.words.zz2F.zzZNc(Unknown Source)
at com.aspose.words.zz2F.zzT(Unknown Source)
at com.aspose.words.zz2L.zzQ(Unknown Source)
at com.aspose.words.zz2L.zzX(Unknown Source)
at com.aspose.words.zzWN.zzZ(Unknown Source)
at com.aspose.words.zzZIE.zzZn0(Unknown Source)
at com.aspose.words.zzZIE.zzZ(Unknown Source)
at com.aspose.words.zzZQF.zzX(Unknown Source)
at com.aspose.words.zzZQF.zzy(Unknown Source)
at com.aspose.words.zzZGR.zzSP(Unknown Source)
at com.aspose.words.zzC3.zzX(Unknown Source)
at com.aspose.words.zzZQX.zzS(Unknown Source)
at com.aspose.words.zzZQF.zzy(Unknown Source)
at com.aspose.words.zzZGR.zzSP(Unknown Source)
at com.aspose.words.zzO1.zzY1(Unknown Source)
at com.aspose.words.zzO2.zzZ(Unknown Source)
at com.aspose.words.zzO2.zza5(Unknown Source)
at com.aspose.words.zz3O.zzZ(Unknown Source)
at com.aspose.words.Document.updatePageLayout(Unknown Source)
at com.aspose.words.Document.zzY5(Unknown Source)
at com.aspose.words.Document.zzaM(Unknown Source)
at com.aspose.words.Document.getPageCount(Unknown Source)
at com.aspose.words.zzDM.zzY(Unknown Source)
at com.aspose.words.zzDM.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at se.sjv.commons.fileconversion.converter.pdfa.DocumentConverter.performConversion(DocumentConverter.java:55)
... 28 more

Sorry, just realised the newest version is 14.6.0.
The above problem shows up when using 14.6.0 aswell.

Hi Eddie,

Thanks for your inquiry. Could you please share to which file format you are converting your shared Docx? I will investigate the issue on my side and provide you more information.

Sorry, wrong code snippet. Here is where the actual call to Aspose compoenents are made:

public void performConversion(final BufferedInputStream inputStream, final OutputStream outputStream)
        throws ConversionException {
    try {
        final LoadOptions loadOptions = new LoadOptions();
        loadOptions.setResourceLoadingCallback(new MyResourceLoadingCallback(getOptions().getBaseUri(),
                getOptions().getUserId()));

        final DocumentBuilder builder = new DocumentBuilder(new Document(inputStream, loadOptions));
        builder.getPageSetup().setPaperSize(PaperSize.A4);
        builder.getDocument().save(outputStream,getSaveOptions());//throws NPE here
    } catch (final Exception e) {
        throw new ConversionException(e);
    }
}

protected PdfSaveOptions getSaveOptions() {
    final PdfSaveOptions options = new PdfSaveOptions();
    options.setCompliance(PdfCompliance.PDF_A_1_B);
    return options;
}

We’re converting to PDF and I’ve marked the line that throws above

Hi Eddie,

Thanks for sharing the detail. I have tested the scenario using following code example and have not found any exception while using latest version of Aspose.Words for Java 14.6.0. I have attached the output Pdf document with this post for your kind reference.

Document doc = new Document(MyDir + "Javariktlinjer+Jordbruksverkets+utvecklingsplattform.docx");
final LoadOptions loadOptions = new LoadOptions();
// loadOptions.setResourceLoadingCallback(new MyResourceLoadingCallback(getOptions().getBaseUri(), getOptions().getUserId()));
final DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setPaperSize(PaperSize.A4);
final PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_A_1_B);
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, options);

Strange, because the stack trace tells me that the exception occurs somewhere in your classes. How can we investigate to try and figure out this bug?

Hi Eddie,

Thanks for your inquiry. Could you please try the following code example at your end and let us know if you still face the exception? Please also share the following detail for investigation purpose.

What environment are you running on?

  • OS (Windows Version or Linux Version)
  • Architecture (32 / 64 bit)
  • Java version
Document doc = new Document(MyDir + "Javariktlinjer+Jordbruksverkets+utvecklingsplattform.docx");
final LoadOptions loadOptions = new LoadOptions();
// loadOptions.setResourceLoadingCallback(new MyResourceLoadingCallback(getOptions().getBaseUri(), getOptions().getUserId()));
final DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setPaperSize(PaperSize.A4);
final PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_A_1_B);
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, options);