Free Support Forum - aspose.com

Converting email to PDF throws NullPointerException on Java

I am trying to convert a mail message to PDF using the following example: https://docs.aspose.com/display/emailjava/Save+Email+Message+As+PDF

    MailMessage eml = MailMessage.load(inputStream);

ByteArrayOutputStream emlStream = new ByteArrayOutputStream();
eml.save(emlStream, SaveOptions.getDefaultMhtml());

LoadOptions lo = new MhtLoadOptions();
InputStream bis = new ByteArrayInputStream(emlStream.toByteArray());
Document doc = new Document(bis, lo);

doc.save(response.getOutputStream(), SaveFormat.PDF);

First, the message is converted to MHTML. That seems to work ok. But when I convert this MHTML to PDF, a NullPointerException is thrown:

Caused by: java.lang.NullPointerException
at com.aspose.pdf.internal.html.rendering.MhtmlRenderer.render(Unknown Source)
at com.aspose.pdf.internal.html.rendering.MhtmlRenderer.render(Unknown Source)
at com.aspose.pdf.internal.html.rendering.MhtmlRenderer.render(Unknown Source)
at com.aspose.pdf.l10j.lI(Unknown Source)
at com.aspose.pdf.ADocument.lI(Unknown Source)
at com.aspose.pdf.ADocument.<init>(Unknown Source)
at com.aspose.pdf.Document.<init>(Unknown Source)

@jphhoeks,

Thanks for the code segment and error detail.

Please zip the email message (.msg file) and attach it here, so we could evaluate your issue precisely to consequently figure it out soon.

I used the wrong Document class (from Aspose PDF instead of Words). Using the correct one solved the problem.

@jphhoeks,

Good to know that your issue is sorted out now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.