Hello,
I’m using Aspose.words for java version : 23.10 and my java version is 17.
I’m using LINQ reporting engine to populate a docx template and to generate a PDF from it.
- The encountered error is :
java.lang.IllegalStateException: An error has been encountered while evaluating the expression or statement ‘riskAddress2.isEmpty() == false]>’. Cannot invoke “Object.getClass()” because “obj” is null
- The docx case is :
<<foreach [in sites]>>
Site n° <<[indexOf()]>>
<<[riskAddress1]>>
<<if [riskAddress2.isEmpty() == false]>><<[riskAddress2]>> <</if>>
<<if [riskAddress3.isEmpty() == false]>><<[riskAddress3]>> <</if>>
<<[riskAddress4]>>
<</foreach>>
- The java code case is :
List<Map<String, Object>> listSiteParameters = new ArrayList<>();
Map<String, Object> siteParameters = new HashMap<>();
siteParameters.put("riskAddress1", "add1");
siteParameters.put("riskAddress2", "add12");
siteParameters.put("riskAddress3", null);
listSiteParameters.add(siteParameters);
siteParameters = new HashMap<>();
siteParameters.put("riskAddress1", "add2");
siteParameters.put("riskAddress2", "");
siteParameters.put("riskAddress3", "");
listSiteParameters.add(siteParameters);
map.put("sites", listSiteParameters);
Can you explain why I have the error in this specific case ?
P.S.
To avoid the error I used the utility method : “org.apache.commons.lang3.ObjectUtils#isNotEmpty (String str)”.
I have registered the ObjectUtils class with the known types mechanism (engine.getKnownTypes().add(ObjectUtils.class)).
@smaisonn
The IllegalStateException you are encountering is likely due to the fact that riskAddress3 is set to null in your data. When the LINQ Reporting Engine evaluates the expression riskAddress3.isEmpty(), it attempts to call a method on a null object, resulting in the exception.
To avoid this error, you can modify your template to check for null values before calling methods on them. For example, you can use a conditional check to ensure that riskAddress3 is not null before checking if it is empty:
<<if [riskAddress3 != null] && [riskAddress3.isEmpty() == false]>><<[riskAddress3]>> <</if>>
This way, the expression will only evaluate isEmpty() if riskAddress3 is not null, preventing the IllegalStateException.
Alternatively, you can use a utility method that handles null checks internally, such as org.apache.commons.lang3.ObjectUtils#isNotEmpty(String str). Ensure that you have registered the ObjectUtils class correctly with the LINQ engine.
If you continue to face issues, consider reviewing the LINQ Reporting Engine documentation for more details on handling null values and expressions.
Ok I’m understood your answer, but in the quasi similar following case, the generation works fine :
List<Map<String, Object>> listSiteParameters = new ArrayList<>();
Map<String, Object> siteParameters = new HashMap<>();
siteParameters.put("riskAddress1", "add1");
siteParameters.put("riskAddress2", "");
siteParameters.put("riskAddress3", null);
listSiteParameters.add(siteParameters);
siteParameters = new HashMap<>();
siteParameters.put("riskAddress1", "add2");
siteParameters.put("riskAddress2", "");
siteParameters.put("riskAddress3", "");
listSiteParameters.add(siteParameters);
map.put("sites", listSiteParameters);
Here I set to empty the value of the parameter “riskAddress2” to the line 4 and the null value is still present. But in this case, the engine succeeded the generation of the document.
Regards,
Moreover, the exception occured on the parameter riskAddress2 not on the parameter riskAddress3.
Regards,
@smaisonn We will check the issue and provide you more information.
@smaisonn,
please try updating your Aspose.Words library to the latest version. You can download it from the official releases page:
Download Aspose.Words for Java 25.9 .
If the problem persists, please create a minimal, reproducible Java application (source code without compilation errors) and attach it here as a zip file for testing. Also, please include any sample document files needed to reproduce the issue.
Hi,
Please found the archived java project in copy here :
Error.zip (30,5 Ko)
P.S. :
The used docx template is in the default package “test” (error-case.docx).
In the project you have to replace the line 37 of class Test.java by a correct absolute path, and launch this class like Java Application.
You will encountered the following error:
Exception in thread "main" java.lang.IllegalStateException: An error has been encountered while evaluating the expression or statement 'riskAddress2.isEmpty() == false]>'. Cannot invoke "Object.getClass()" because "obj" is null
at com.aspose.words.internal.zzZDg.zzYkb(Unknown Source)
at com.aspose.words.internal.zzZDg.zzSv(Unknown Source)
at com.aspose.words.internal.zzWgr.zzZWW(Unknown Source)
at com.aspose.words.internal.zzWgr.zzYSg(Unknown Source)
at com.aspose.words.internal.zzWgr.zzYkb(Unknown Source)
at com.aspose.words.internal.zzY1z.zzxE(Unknown Source)
at com.aspose.words.internal.zzWfr.zzYkb(Unknown Source)
at com.aspose.words.internal.zzWIe.zzYkb(Unknown Source)
at com.aspose.words.internal.zzY1z.zzxE(Unknown Source)
at com.aspose.words.internal.zzWfr.zzYkb(Unknown Source)
at com.aspose.words.internal.zzWVQ.zzYkb(Unknown Source)
at com.aspose.words.internal.zzY1z.zzxE(Unknown Source)
at com.aspose.words.internal.zzWfr.zzYkb(Unknown Source)
at com.aspose.words.internal.zzVWg.zzW8k(Unknown Source)
at com.aspose.words.internal.zz22.zzYkb(Unknown Source)
at com.aspose.words.internal.zz22.zzYkb(Unknown Source)
at com.aspose.words.internal.zz22.zzYkb(Unknown Source)
at com.aspose.words.ReportingEngine.buildReport(Unknown Source)
at com.aspose.words.ReportingEngine.buildReport(Unknown Source)
at com.aspose.words.ReportingEngine.buildReport(Unknown Source)
at test.Test.main(Test.java:35)
Caused by: java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "obj" is null
at java.base/java.lang.reflect.Method.invoke(Method.java:561)
at com.aspose.words.internal.zzGV.zzYkb(Unknown Source)
at com.aspose.words.internal.zzGV.zzYkb(Unknown Source)
at com.aspose.words.internal.zzGV.getValue(Unknown Source)
at com.aspose.words.internal.zzZfv.zzXBP(Unknown Source)
at com.aspose.words.internal.zzZjP.zzYkb(Unknown Source)
at com.aspose.words.internal.zzYzj.zzSv(Unknown Source)
at com.aspose.words.internal.zzXBF.zzVYA(Unknown Source)
at com.aspose.words.internal.zzXBF.zzSv(Unknown Source)
at com.aspose.words.internal.zzZDg.zzSv(Unknown Source)
... 19 more
@smaisonn
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSJAVA-3205
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.