Processing document with embedded fonts throws NullPointerException using Java

Hello,
In Aspose.Words for Java v. 10.2 we got the following exception

java.lang.NullPointerException
at asposewobfuscated.gp.d(ExternalFontCache.java: 126)
at asposewobfuscated.fx.d(DocumentFontCache.java: 36)
at asposewobfuscated.gt.a(FontCacheBase.java: 21)
at com.aspose.words.n.a(AttributeConverter.java: 41)
at com.aspose.words.fk$b.E(DocumentRunSplitter.java: 496)
at com.aspose.words.fk$b.moveNext(DocumentRunSplitter.java: 468)
at com.aspose.words.fk.a(DocumentRunSplitter.java: 201)
at com.aspose.words.aot.Wk(RunWriter.java: 171)
at com.aspose.words.aot.a(RunWriter.java: 139)
at com.aspose.words.fk.(DocumentRunSplitter.java: 78)
at com.aspose.words.arg.a(SpanGenerator.java: 226)
at com.aspose.words.fl.ao(DocumentSpanConverter.java: 653)
at com.aspose.words.fl.CE(DocumentSpanConverter.java: 593)
at com.aspose.words.fl.moveNext(DocumentSpanConverter.java: 168)
at com.aspose.words.fl.Ca(DocumentSpanConverter.java: 204)
at com.aspose.words.vp.a(LayoutDocument.java: 38)
at com.aspose.words.Document.updatePageLayout(Document.java: 1416)
at com.aspose.words.le.perform(ExternalActionUpdateLayout.java: 25)
at com.aspose.words.ol.execute(FieldUpdateExecutionItem.java: 79)
at com.aspose.words.oo.hm(FieldUpdater.java: 97)
at com.aspose.words.oo.Jr(FieldUpdater.java: 84)
at com.aspose.words.yj.UL(MailMergeRegion.java: 240)
at com.aspose.words.yj.a(MailMergeRegion.java: 71)
at com.aspose.words.MailMerge.execute(MailMerge.java: 253)
at com.aspose.words.MailMerge.execute(MailMerge.java: 278)

Unfortunately I cannot provide you the document as this was a dynamic one and it is not stored.
Could you please analyze the stack trace and inform what may be the origin of the error?
Best regards, Evgeniy

Hi Evgeniy,

Thanks for your query. Please use the latest version of Aspose.Words for Java 10.8.0 and let us know, If you still face problem.

Hi Tahir,
We cannot right now to upgrade to the newest version. Could you please analyze the code of v.10.2 and tell me when the error is thrown? Which object is null? For example, is it the problem with missing font? Any input from your side is welcome.
Best regards, Evgeniy

Hi Evgeniy,

Please share a sample code which throws the shared exception so that we can reproduce this issue at our end. After that we will try to share a work around for this issue. It would be great, If you share; what you want to do by using Aspose.Words?

Hello,
We could reproduce the same error in Aspose.Words 11.0.0:
The problem occurs when the document containing an embedded True Type font is in processing by two separate Java threads simultaneously.
--------------------------------------------------------------

java.lang.NullPointerException
at asposewobfuscated.jl.d(ExternalFontCache.java: 153)
at asposewobfuscated.io.d(DocumentFontCache.java: 75)
at asposewobfuscated.io.a(DocumentFontCache.java: 43)
at com.aspose.words.o.a(AttributeConverter.java: 41)
at com.aspose.words.fn$b.S(DocumentRunSplitter.java: 460)
at com.aspose.words.fn$b.moveNext(DocumentRunSplitter.java: 432)
at com.aspose.words.fn.a(DocumentRunSplitter.java: 177)
at com.aspose.words.aqq.c(RunWriter.java: 305)
at com.aspose.words.aqq.aca(RunWriter.java: 169)
at com.aspose.words.aqq.a(RunWriter.java: 137)
at com.aspose.words.fn.(DocumentRunSplitter.java: 76)
at com.aspose.words.ati.a(SpanGenerator.java: 230)
at com.aspose.words.fo.aJ(DocumentSpanConverter.java: 752)
at com.aspose.words.fo.Hx(DocumentSpanConverter.java: 692)
at com.aspose.words.fo.moveNext(DocumentSpanConverter.java: 171)
at com.aspose.words.fo.GU(DocumentSpanConverter.java: 207)
at com.aspose.words.wv.a(LayoutDocument.java: 42)
at com.aspose.words.Document.updatePageLayout(Document.java: 1468)
at com.aspose.words.lh.perform(ExternalActionUpdateLayout.java: 25)
at com.aspose.words.pg.execute(FieldUpdateExecutionItem.java: 79)
at com.aspose.words.pj.iC(FieldUpdater.java: 97)
at com.aspose.words.pj.Om(FieldUpdater.java: 84)
at com.aspose.words.zp.aay(MailMergeRegion.java: 278)
at com.aspose.words.zp.a(MailMergeRegion.java: 70)
at com.aspose.words.MailMerge.execute(MailMerge.java: 254)
at com.aspose.words.MailMerge.execute(MailMerge.java: 279)

Best regards, Evgeniy

Hi Evgeniy,

Thanks for your inquiry. You should note that Aspose.Words component does support multi-threading. However, the only thing you need to make sure is that you always use separate Document instances per each thread. One thread should use one Document object. Please fix your code and let us know your findings then.

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi Awais,
Yes, we know that Aspose declares a support of multi-threading. We do use completely separate thread with separate Document instances per each thread.
That’s why I ask you to share the problem with development team and clarify why the exception happens.
Please check how fonts are managed by Aspose.Words. What is the class ExternalFontCache which throws the exception?
Best regards, Evgeniy

Hi
Evgeniy,

Thanks for the additional information. But, could you please share your code to be able to reproduce this exception on my side? I will investigate the issue further and provide you more information.

Best Regards,

Hi Awais,
Unfortunately I cannot provide the code sample as the error happens only in Production. In our artificial tests we could not reproduce the error.
So I kindly ask you to review the code of ExternalFontCache.java and explain in these cases the error can be thrown.
Best regards, Evgeniy

Hi Evgeniy,

Thanks for the additional information. I have asked for any possible cause of this problem from our development team. As soon as, any information is shared by them, I will be more then happy to share that with you.

Evgeniy:
The problem occurs when the document containing an embedded True Type font is in processing by two separate Java threads simultaneously.

Could you please explain your problem in more detail e.g. what kind of document processing tasks are you trying to perform and tell us about your production environment (e.g. Which hosting server you are using? What version of which OS is being used?) etc?

We apologize for your inconvenience.

Best Regards,

Hi Awais,
We have a Word document (template) containing 20 merge fields and one embedded font.
Then this document is fiiled with some data and appended to global output document. See the sample below.

Document globalDocument = new Document();
for (int i = 0; i <100; i++)
{
    Document tmp = new Document(templateName);

    // get fields and value according to iteration index i
    tmp.getMailMerge().execute(names, values);

    globalDocument.append(tmp, ImportFormatMode.USE_DESTINATION_STYLES);
}

For one specific merge field there is a callback which adds a style containing a font name of the font embedded into the template.

Node node = mergeFieldEventArgs.getField().getStart().getAncestor(NodeType.PARAGRAPH);
Paragraph p = (Paragraph) node;
Style s = p.getDocument().getStyles().add(StyleType.PARAGRAPH, "style_number_" + i);
i++;
s.getFont().setName("MyEmbeddedFontName");
s.getFont().setSize(10);
p.getParagraphFormat().setStyle(s);

This works properly until the the processing procedure run simultaneously (the same time in two separate Java threads). In one thread we get the exception, but the second thread completes its work successfully and returns the result file.
OS: Windows 7, Windows 2003 Server
Java: JDK 1.6.27, JDK 1.6.29, JDK 1.6.30
Best regards,

Hi Evgeniy,

Thanks for the additional information. We’re working over your query and will get back to you soon.

Best Regards,

Hello,
Any news here?
Best regards, Evgeniy

Hi Evgeniy,

Thanks for your inquiry. Unfortunately, this issues is not resolved yet. However, the responsible developer has already started analyzing this issue. It is in analysis phase. We will keep you informed regarding the status of this issue and let you know once it is resolved.

Best Regards,

The issues you have found earlier (filed as WORDSNET-5902) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hello,
In 11.1.0 there is the same effect but with another stack trace.
We work with Aspose library in two separate threads at the same time and we get the exception in one thread. The second task completed successfully and gives the result file properly.

java.lang.NullPointerException
at asposewobfuscated.kc.d(Unknown Source)
at asposewobfuscated.ip.d(Unknown Source)
at asposewobfuscated.ip.a(Unknown Source)
at com.aspose.words.aqs.c(Unknown Source)
at com.aspose.words.aqs.aeC(Unknown Source)
at com.aspose.words.aqs.a(Unknown Source)
at com.aspose.words.fn.(Unknown Source)
at com.aspose.words.atj.a(Unknown Source)
at com.aspose.words.fo.aO(Unknown Source)
at com.aspose.words.fo.JW(Unknown Source)
at com.aspose.words.fo.moveNext(Unknown Source)
at com.aspose.words.fo.Js(Unknown Source)
at com.aspose.words.ww.a(Unknown Source)
at com.aspose.words.Document.updatePageLayout(Unknown Source)
at com.aspose.words.lh.perform(Unknown Source)
at com.aspose.words.ph.execute(Unknown Source)
at com.aspose.words.pk.iY(Unknown Source)
at com.aspose.words.pk.QM(Unknown Source)
at com.aspose.words.zr.acZ(Unknown Source)
at com.aspose.words.zr.a(Unknown Source)
at com.aspose.words.MailMerge.execute(Unknown Source)

Best regards, Evgeniy

Hello Evgeniy,

Could you please try to initialize font cache in single thread before running document processing in multiple threads? Font cache could be initialized by saving any document to any fixed-page formats such as PDF, XPS, Images etc.
Let us know if this workaround works for you.

Hello Konstantin,
That did not help. I tried to register fonts and save an empty document to PDF right after an application started. The effect is the same.
Do you see other improvements which you can be added to Aspose code? Can you describe what is cached now in the ExternalFontCache and how it works in case of multi threading?
Best regards, Evgeniy

Hello Evgeniy,

That did not help.I tried to register fonts and save an empty document

to PDF right after an application started. The effect is the same.

This is strange. That workaround should help for all potential reasons of NPE I could see.
Could you please try to add some text to the document just to make sure that font cache will be initialized?
Or maybe you update font sources from threads by calls to Aspose.Words.Fonts.FontSettings class methods? That would explain why workaround didn’t help.

Do you see other improvements which you can be added to Aspose code? Can you describe what is cached now in the ExternalFontCache and how it works in case of multi threading?

ExternalFontCache class stores parsed font data from sources specified by FontSettings.SetFontSources or FontSettings.SetFontFolders calls. It is a static cache and uses synchronization for working in case of multithreading. Probably there is a problem with porting of synchronization code to Java.
Curretly I can’t see any improvements which could solve the problem. But I will discuss it with our Java porting developer.

Hello Konstantin,

I added some text to the initialization document which is saved in the starting thread.
Unfortunately this did not help - two parallel threads again throw the exception.

Could you please discuss this with Java developer - maybe synchronizaton block of code need be extented or refactored?

Also I would like to notice that our Word document contains an embedded font for which a system TTF file exists in the meantime. We try to have them synchronized (the same font files) but potentially they can be different. Do you see some problems here? Which font do you place to the cache - from document or from system font folder?

Best regards, Evgeniy