Default value for null fields in a Jasper Report
Solution #1
Your solution:
- Use a regular Double field (
doubleField
) for the column value. - Add a static String text field at the same location.
- Change the Double field to Blank When Null.
- Set the PrintWhenExpression value for the String text field to:
$F{doubleField} == null
.
Solution #2
The problem is, as you pointed out, that a Double and a String are two different data types. You can assign a String variable to the value of the Double using an appropriate expression. Then use the String variable as the field. The expression might resemble:
($F{doubleField} == null) ?
"N/A" : new java.text.DecimalFormat("#.##").format($F{doubleField})
(Note: My preference is to use ==
instead of !=
. Think positive.)
Solution #3
Change the SQL statement to pre-format the Double as a text string, and use the "N/A" in the string (by using a CASE
or DECODE
statement in the query).
Avoid this solution, though, as it is not maintainable.
Recommendation
Do not hard-code the "N/A" string throughout the report(s); put the "N/A" text in a constant, or a parameter with a default value of "N/A".