Default value for null fields in a Jasper report

Background

ResultSet has many Double value fields (with patterns such as "###0.000" ). Some values ​​may be null .

Problem

I want to replace null values ​​with "N/A" , which is a String and cannot print in a Double field. Printing "0.00" for null values ​​is not acceptable.

Using the value of PrintWhenExpression ($F{value} != null) ? $F{value} : "N/A" ($F{value} != null) ? $F{value} : "N/A" does not work; therefore, templates cannot be used.

Idea

Add hidden fields that write "N / A". These fields will be printed only if null .

Question

Is there a better solution, and if so, what is it?

Thanks.

+6
jasper-reports ireport textfield
source share
2 answers

Solution No. 1

Your choice:

  • Use a regular double field ( doubleField ) for the column value.
  • Add a text field for static string text in the same place.
  • Change the double field to Empty when Null .
  • Set the PrintWhenExpression value for the String text field: $F{doubleField} == null .

Decision No. 2

The problem is, as you pointed out, that Double and String are two different data types. You can set the String variable to Double using the appropriate expression. Then use the String variable as the field. The expression may resemble:

 ($F{doubleField} == null) ? "N/A" : new java.text.DecimalFormat("#.##").format($F{doubleField}) 

(Note. My preference is to use == instead of != . Think positive.)

Decision No. 3

Modify the SQL statement to pre-format Double as a text string, and use "N / A" in the string (using the CASE or DECODE in the query).

Avoid this solution as it is not supported.

Recommendation

Do not print the string "N / A" in all reports; put the text "N / A" in a constant or parameter with the default value of "N / A".

+10
source share

You can try something like this in your field expression:

 ("Your static text "+(($F{field}!=null)?$F{field}:"")) 

Or, if you do not want your static text to be visible when the field is empty, try putting $F{field}!=null PrintWhenExpression in your PrintWhenExpression .

0
source share

All Articles