NullPointerException at com.aspose.pdf.internal.p68.z30.m44(Unknown Source)

Hello Aspose-Team,

I tried to update to the latest version (17.4), but this is not possible.
Since Version 11.8.0 (Versions before 11.6.0 and 11.7.0 are working) i am getting a NullpointerException.

Stacktrace with version 11.8.0:

java.lang.NullPointerException
at com.aspose.pdf.internal.p68.z30.m44(Unknown Source)
at com.aspose.pdf.TextState.setFont(Unknown Source)
at com.aspose.pdf.TextState.m1(Unknown Source)
at com.aspose.pdf.TextState.m1(Unknown Source)
at com.aspose.pdf.TextSegment.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.appendText(Unknown Source)

With aspose-version 17.4 i got the following stacktrace:

java.lang.NullPointerException
at com.aspose.pdf.internal.p71.z31.m48(Unknown Source)
at com.aspose.pdf.Copier.m1(Unknown Source)
at com.aspose.pdf.Copier.m1(Unknown Source)
at com.aspose.pdf.Copier.duplicate(Unknown Source)
at com.aspose.pdf.Copier.duplicate(Unknown Source)
at com.aspose.pdf.internal.p65.z6.m1(Unknown Source)
at com.aspose.pdf.TextState.setFont(Unknown Source)
at com.aspose.pdf.TextState.m1(Unknown Source)
at com.aspose.pdf.TextState.m1(Unknown Source)
at com.aspose.pdf.TextSegment.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.appendText(Unknown Source)

Code-Snippet:

TextFragment tf = getTextFragment(properties.getLabels().readLabel(“hint”) + ": ", MyFont, FONTSIZE_TEXT, c646464);
tf.setPosition(new Position(X_POS_LABEL, yPosText));
tf.getTextState().setBackgroundColor(cf0f0f0);
tf.getRectangle().setURX(200);
textBuilder.appendText(tf); // —> NullpointerException


What´s wrong ? Could you help me, please ?
Thanks in advance!

Best Regards

Matthias

Hi Matthias,

Thanks for contacting support.

I have tested your code snippet with latest version of Aspose.Pdf for Java like following, in our environment, and I was unable to notice the exception which you have reported.

Document doc = new Document();
Page page = doc.getPages().add();
TextFragment tf = new TextFragment("Hello World");
tf.setPosition(new Position(100, 100));
tf.getTextState().setBackgroundColor(com.aspose.pdf.Color.getBlack());
tf.getRectangle().setURX(200);
TextBuilder textBuilder = new TextBuilder(page);
textBuilder.appendText(tf);

We will really appreciate if you please share more details i.e complete code snippet and sample input document, so that we can test the scenario in our environment and share our findings with you accordingly.

Best Regards,

Hi Ali,

here´s a more detailed example code, where i am able to reproduce the NullPointerException every time. (Note that version 11.6.0, 11.7.0 works. With version 11.8.0 a NullPointerException will occur)

The Code snippet testet with version 11.8.0 is:

stampDoc = new Document();
Page page = stampDoc.getPages().add();
boolean isLandscape = true;
isLandscape = true;
PageInfo pageInfo = page.getPageInfo();
pageInfo.setLandscape(isLandscape);
int columnWidths = 742;
HeaderFooter header = new HeaderFooter();
Table headerTable = new Table();
headerTable.setColumnWidths(""+ columnWidths);
header.getParagraphs().add(headerTable);
Row row = headerTable.getRows().add();

customerFont = FontRepository.findFont(“CustomerFontName”);

Cell cell = row.getCells().add();
cell.setAlignment(HorizontalAlignment.Center);
cell.getParagraphs().add(creator.getTextFragment(" “, customerFont, 25));
cell.getParagraphs().add(creator.getTextFragment(“xyf dssad”, customerFont, 12));
page.setHeader(header);
stampDoc.convert(tgtFileName + “-cover-conversion.log”, 7, ConvertErrorAction.Delete);
stampDoc.save(tgtFileName + “-stamp-document_1.pdf”);
stampDoc.close();

Document nextStampDoc = new Document();
Page p = nextStampDoc.getPages().add();
Table t = new Table();
t.setColumnWidths(”"+ columnWidths);
HeaderFooter h = new HeaderFooter();
h.getParagraphs().add(t);
Row r = t.getRows().add();
Cell c = r.getCells().add();
c.setAlignment(HorizontalAlignment.Center);
c.getParagraphs().add(creator.getTextFragment(" ", customerFont, 25));
c.getParagraphs().add(creator.getTextFragment(“bla bla”, customerFont, 12));
p.setHeader(h);
nextStampDoc.convert(tgtFileName + “-cover-conversion.log”, 7, ConvertErrorAction.Delete); // --> **NullPointerException at com.aspose.pdf.internal.p68.z30.m44(Unknown Source)**
nextStampDoc.save(tgtFileName + “-stamp-document_2.pdf”);
nextStampDoc.close();

What is the real name of variable “m44” ?

Here´s the complete aspose-stacktrace part:

java.lang.NullPointerException
at com.aspose.pdf.internal.p68.z30.m44(Unknown Source)
at com.aspose.pdf.TextState.setFont(Unknown Source)
at com.aspose.pdf.TextState.m1(Unknown Source)
at com.aspose.pdf.TextState.m1(Unknown Source)
at com.aspose.pdf.TextSegment.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.m1(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.z152.m1(Unknown Source)
at com.aspose.pdf.z152.m8(Unknown Source)
at com.aspose.pdf.Cell.m1(Unknown Source)
at com.aspose.pdf.Row.m1(Unknown Source)
at com.aspose.pdf.Table.m1(Unknown Source)
at com.aspose.pdf.z152.m8(Unknown Source)
at com.aspose.pdf.Page.m1(Unknown Source)
at com.aspose.pdf.Page.m6(Unknown Source)
at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
at com.aspose.pdf.Document.processParagraphs(Unknown Source)
at com.aspose.pdf.ADocument.m24(Unknown Source)
at com.aspose.pdf.ADocument.m1(Unknown Source)
at com.aspose.pdf.ADocument.convert(Unknown Source)
at com.aspose.pdf.Document.convert(Unknown Source)
at com.aspose.pdf.ADocument.convert(Unknown Source)
at com.aspose.pdf.Document.convert(Unknown Source)

Best Regards
Matthias

Hi Matthias,

Thanks for sharing more details.

I have tried your code snippet with latest version of the API (Aspose.Pdf for Java 17.4.0) and was able to observe the similar exception which you have shared. However when I shifted the line “Document.close()”, at the end of the code, the exception did not occur and outputs were generated correctly. Please check the following complete code snippet which I have tried. I have also attached the generated output(s) for your reference.

public void Test()
{

    Document stampDoc = new Document();

    Page page = stampDoc.getPages().add();

    boolean isLandscape = true;

    isLandscape = true;

    PageInfo pageInfo = page.getPageInfo();

    pageInfo.setLandscape(isLandscape);

    int columnWidths = 742;

    HeaderFooter header = new HeaderFooter();

    Table headerTable = new Table();
    headerTable.setColumnWidths("" + columnWidths);

    header.getParagraphs().add(headerTable);

    Row row = headerTable.getRows().add();

    com.aspose.pdf.Font customerFont = FontRepository.findFont(“Arial”);

    Cell cell = row.getCells().add();

    cell.setAlignment(HorizontalAlignment.Center);

    cell.getParagraphs().add(getTextFragment(" ", customerFont, 25));

    cell.getParagraphs().add(getTextFragment(“xyf dssad”, customerFont, 12));

    page.setHeader(header);

    stampDoc.convert(dataDir + “cover - conversion.log”, 7, ConvertErrorAction.Delete);

    stampDoc.save(dataDir + “stamp - document_1.pdf”);

    //stampDoc.close();

    Document nextStampDoc = new Document();

    Page p = nextStampDoc.getPages().add();

    Table t = new Table();

    t.setColumnWidths("" + columnWidths);

    HeaderFooter h = new HeaderFooter();

    Row r = t.getRows().add();

    Cell c = r.getCells().add();

    c.setAlignment(HorizontalAlignment.Center);

    c.getParagraphs().add(getTextFragment(" ", customerFont, 25));

    c.getParagraphs().add(getTextFragment(“bla bla”, customerFont, 12));
    h.getParagraphs().add(t);
    p.setHeader(h);

    nextStampDoc.convert(dataDir + “cover - conversion2.log”, 7, ConvertErrorAction.Delete);

    nextStampDoc.save(dataDir + “stamp - document_2.pdf”);

    nextStampDoc.close();

    stampDoc.close();

}

public TextFragment getTextFragment(String text, com.aspose.pdf.Font font, float size)

{

    TextFragment tf = new TextFragment(text);

    tf.getTextState().setFont(font);

    tf.getTextState().setFontSize(size);

    return tf;

}

It seemed that some of the API internal methods were creating conflicts with Java Language Run-time, so I have logged an investigation ticket as PDFJAVA-36756 in our issue tracking system. For now you can use workaround which I have shared above. We will further investigate the reason and keep you updated on the status of resolution. Please be patient and spare us little time.

mathias.frenzel:

I tried to update to the latest version (17.4), but this is not possible.

Please note that we always encourage and recommend to use latest version of the API as it contains all enhancements. Would you please share the reasons about not updating to latest version?

We are sorry for the inconvenience.

Best Regards,

Hi Ali,



thanks for the Workaround.



Note that there´s another Workaround… and it seems that the Problem has something to do with the “font” object. If you use the “FontRepository.findFont” method a second time before the call of “doc.convert…” it works also. Additionally the member variables of the font object are changed after calling the “convert” method…



Why we do not update to the latest ? Bug described above…


Is there a possibility to watch the bug status of PDFJAVA-36756 ?
So it would be nice to get a notification when it´s fixed and in which release version.

Best Regards


Matthias

Hi Mathias,

Thanks for writing back.

I would like to share with you that the investigation ticket PDFJAVA-36756 has been resolved and product team has shared their feedback.

mathias.frenzel:

Note that there´s another Workaround… and it seems that the Problem has something to do with the “font” object. If you use the “FontRepository.findFont” method a second time before the call of “doc.convert…” it works also. Additionally the member variables of the font object are changed after calling the “convert” method…

The reason of exception was that when you were closing first document, all initialized objects (including customerFont object) were disposing, as they were linked to first document. You need to reinitialize customerFont after closing the first document like following:

stampDoc.close();
customerFont = FontRepository.*findFont*("Arial");
Document nextStampDoc = **new** Document();

Please update to latest version Aspose.Pdf for Java 17.4.0 as latest version contains all fixes and enhancement and we strongly recommend to use that. In case of any further assistance, please feel free to contact us.

Best Regards,

Hi Ali,

so my described workaround is the solution now ? Really ?
Why is it not possible to avoid the NullpointerException in generelly on aspose side ?
And why the behavior has been changed ? Because with version 11.6.0 everthing works very well.
In my opinion its totally ineffective/unperformant to “search/find” the font every time from operating system…

Best Regards

Matthias

Hi Mathias,

Thanks for writing back.

Generally it is a good practice to dispose an object after it is no longer needed and in case if that object uses some resources and they are not being disposed with that object, this leads to memory leaks.

In Aspose.Pdf DOM model, you work with a complete document object which contains full hierarchy of classes and it uses many unmanaged resources so as I said earlier that if resources used by an object, are not disposed with it, it may lead to memory leaks. Which is why this new behavior of the API has been implemented.

However, you may use try-finally approach (just like using statement in C#.NET) to avoid NullPointerException and make sure that all objects are disposed after they are no longer of use like following:

Document stampDoc = new Document();< o:p ></ o:p >
Document nextStampDoc = new Document();
try
{
    // do your stuff here…
}
finally
{
    stampDoc.close();
    nextStampDoc.close();
}

Moreover, we have shared your above concerns/comments with product team and as soon as we have some feedback from them, we will let you know.

Best Regards,

Hi Mathias,


We have received feedback from development team and I would like to share it with you as well.

It was a forced measure as on some situations with more complicated code, a garbage collector on some JVM (basically on Linux operation systems) not always correctly close the file descriptors, associated with these objects and this resulted in a large number of open font files. Now all these problems are fixed.

Note that font searching is very good optimized in our library (paths to fonts are cached) so we do not expect any performance problems. Please, see the code bellow with 100,000 iterations that spends only one second.

long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
Font customerFont = FontRepository.findFont(“Arial”);
}
System.out.println(“Spending time: " + (System.currentTimeMillis() - start) + " ms”);

OUTPUT=> Spending time: 1007 ms


Please keep using our API and in case of any further assistance, please feel free to contact us.


Best Regards,

Hi Ali,

thanks for the detailed answers.

Best Regards

Matthais

Hi Mathias,


Thanks for your feedback. Please keep using our API and in case of any further assistance, please feel free to contact us.


Best Regards,