xdocreport

In XDocReport, how to handle null value?


Is there a way to handle null value for a field in XDocReport? or do I need to manipulate it on my own? example:

if (thisVar == null)
  context.put("sampleText", "");
else
  context.put("sampleText", thisVar);

or is there an option in docx quick parts?

I found this line in the error message of XDocReport. However I could not understand where to apply this, in the template or in the code.

Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use [#if myOptionalVar??]when-present[#else]when-missing[/#if]. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??


Solution

  • In docx, append ?if_exists to the field name

    «${tx.amount?if_exists}»

    you may also append !

    «${tx.amount!}»

    Please refer to this link for those who uses freemarker. How to check if a variable exists in a FreeMarker template?