Search code examples
jasper-reports

How do I replace a null value with 0?


I have a report, with the fields A and B coming from my database. In the select statement sometime A and B are null.

Problem is when I ask for the group sum of A - B and one of the record has a null in it the total for the sum is null.

How do I get around this?


Solution

  • In jasper-reports variable expression you can use ternary operator

    ($F{value1}!=null?$F{value1}:0)
    

    which means if value1 is different from null then use value1 otherwise use 0

    jrxml variable declaration example

    <variable name="varWithTernary" class="java.lang.Double" calculation="Sum">
        <variableExpression><![CDATA[($F{value1}!=null?$F{value1}:0)-($F{value2}!=null?$F{value2}:0)]]></variableExpression>
    </variable>
    

    With these kind of statements your are able to handle null value as you like