IReport subreport return value

ireportreturn-valuesubreport

I am using iReport 4.0.2 and I want to show a result in my main report. For example, in my main report we have two columns and I want to get the sum of that two columns, just like this format:

A      B     sum
10     5      15

Where A is one field in the main report and B is a return value of my subreport.
This works well. But, the key point is that sometimes subreport will not return any value, which is the problem. In this case, the result of sum is like this:

A       B      sum
10             NULL

As we see here, B is the subreport return value but it's value neither NULL nor 0 . That's why we have that problem.

I try to find how can I get the return value from subreport when the SQL returns no results. I know iReport has a property named 'When No Data', but it doesn't help.
So I want to know, whether we have another way to solve the problem in iReport or using some SQL skills.

Best Answer

I think that in your second example B is actually null but perhaps your text field has the 'Blank When Null' property set. I was able to reproduce your results except for the blank for B.

The real issue is that you can't add or subtract with a null value. JasperReports will just print null. You have to add some logic to provide a default value if one of the variables in the expression is null. For example

$F{MAIN_REPORT_FIELD} - ($V{SUB_RESULT} == null ? 0 : $V{SUB_RESULT})

Unfortunately, this is broken for reports that use Groovy for expressions in JasperReports 4.0.2 (Case 0005138) and will always return null.

You have a few options:

  1. You could upgrade to JasperReports/iReport 4.1.1 which resolves this issue.
  2. You could switch to using Java for expressions.

    Depending on your report this will either be painless or it will be a chore. Any fields that don't have the correct Expression Class will have to be fixed up and you'll probably have to be more explicit in any expression that performs operations on non primitive/wrapper types.

Related Topic