Nullpointer when converting word to pdf


I am currently using:

  • gradle:
  •   implementation(group: 'com.aspose', name: 'aspose-pdf', version: '22.12')    
  •   implementation(group: 'com.aspose', name: 'aspose-words', version: '17.2.0')
  • java: openjdk 11
  • windows 10
  • jboss eap 7.4.8

My problem i get an exception when i try to convert a docx file to a pdf file. The exception:

Caused by: java.lang.NullPointerException
at com.aspose.pdf.ADocument.preSave(Unknown Source)
at com.aspose.pdf.ADocument.lf(Unknown Source)
at Source)
at Source)
at com.aspose.pdf.ADocument$4.lI(Unknown Source)
at com.aspose.pdf.internal.l89u.lf.lI(Unknown Source)
at com.aspose.pdf.internal.l89u.lj.lI(Unknown Source)
at Source)
at Source)

This exception happens only to the docx file i attached to this thread. Other docx files seemed to work. xxxxxx1.docx (22.4 KB)

The code:

private ByteArrayOutputStream ladeWordAndAddToDocument(final InputStream 
              dateiDatenStream, final com.aspose.pdf.Document ergebnisDoc)
			throws Exception
		ByteArrayInputStream pdfByteInputStream = null;

		try (ByteArrayOutputStream pdfOutStream = new ByteArrayOutputStream();)
			final com.aspose.words.Document asposeEinzufuegendesDocument = new com.aspose.words.Document(
					dateiDatenStream);, SaveFormat.PDF);

			pdfByteInputStream = new ByteArrayInputStream(pdfOutStream.toByteArray());
			com.aspose.pdf.Document pdfDocument = new Document(pdfByteInputStream);
				final PageCollection pageCollection = pdfDocument.getPages();

			ByteArrayOutputStream output = new ByteArrayOutputStream();;
			return output;
		} finally
			if (pdfByteInputStream != null)

Can you tell me why i am getting a NullpointerException, because other files are working.
The same error happens also to some pdf files, when i try to get a byte[] of a pdf file

pdf file which throws the same error.



It looks like that code is corrupting converted PDF document. Can you please use 23.1 version of both APIs and try to save the PDF to a physical location as file using Aspose.Words? In case the issue still persists and exception still occurs, please let us know. We will further proceed to assist you accordingly.


i am using now the latest 23.1 version for both apis (pdf and word). If i save the word to a pdf file on the file system, it works. It generates a proper valid pdf file.

But when i convert the word into a pdf file in memory (ByteArrayOutputStream) and then merge the pdf file i still get the NullpointerException.

So unfortunately its not fixed.


We used below code snippet in our environment to test the case and did not notice any issues:

com.aspose.words.Document doc = new com.aspose.words.Document(dataDir + "xxxxxx1.docx");

ByteArrayOutputStream dstStream = new ByteArrayOutputStream();, com.aspose.words.SaveFormat.PDF);

byte[] dataBytes = dstStream.toByteArray();
ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes);
com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document(byteStream);
// You can initialize another existing PDF below using input stream as well
com.aspose.pdf.Document pdf2 = new com.aspose.pdf.Document();
ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();;

You can please check the code and modify it so that we can again try to replicate the issue that you are facing and proceed further to assist you.

Thanks for your help so far. I refactored my code in a different way, so that the converted pdf document is directly returned to the user and not merged into a different pdf. This solves the issue.


It is nice to know that your issue has been resolved. Please keep using the API and feel free to create a new topic in case you need further assistance.