Jump to content
We've recently updated our Privacy Statement, available here ×

dealing with null values


rossi_jeff

Recommended Posts

 I my application I have several events tables that store duration in minutes. When the event is in progress the duration is null.

I my report I want to show the text "In Progress" or the formatted value, in csv the formatted value is integer

$V{duration_hours} -> integer

($F{stop_events_duration}.intValue() / 60).intValue()

 

$V{duration_minutes} -> integer

($F{stop_events_duration}.intValue() % 60)

 

$V{duration_hrs_min} -> type string

(($F{stop_events_duration}.equals(null)) || ($V{duration_hours}.equals(null)) || ($V{duration_minutes}.equals(null))) ? "In Progress" : (($V{duration_hours} == 0) ? ($V{duration_minutes}.toString() + " min") : ($V{duration_hours}.toString() + ($V{duration_hours} == 1 ? " hr, " : " hrs, ") + $V{duration_minutes}.toString() + " min"))

 

The textfield in details

($F{stop_events_duration}.intValue() == null) ? "In Progress" : (($P{output_format} == "PDF") ? $V{duration_hrs_min} : ($P{output_format} == "RTF") ? $V{duration_hrs_min} : $F{stop_events_duration}.toString())

 

 

Link to comment
Share on other sites

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

Perhaps you run into trouble with this line:

$F{stop_events_duration}.intValue() == null

I expect that it makes mores sense to do this:

$F{stop_events_duration} == null

If the field is null, then calling intValue() on it won't work. You need to check on the object itself. Does that change anything?

Regards,
Matt

 

Link to comment
Share on other sites

Can you post a minimal .jrxml example that shows the problem? Something like this:

"Expected outcome is 'In Progress':" [some expression using MyIntegerField]
"Expected outcome is 'In Progress':" [analogous expression using MyStringField]

If it's easily reproduceable like that then I expect that it should be easy to fix.

Regards,
Matt

Link to comment
Share on other sites

Yes, please report this as a bug.

I can easily reproduce it with your sample. You can attach my .jrxml to the bug report to save having to find your own copy.

It's definitely a bug linked to the upgrade of Groovy. I can switch the report language from Groovy to Java and I get the expected behavior.

Thanks,
Matt

Link to comment
Share on other sites

  • 2 months later...

Hi,

 

Sorry for my late intervention on this thread, but we were actually aware of this problem and worked to solve in the meantime.

 

The problem appears in Jaspersoft v.4.0 because of a Groovy upgrade we made at that time. The new version of Groovy brought changes in how Groovy expressions work. The most obvious one was with regards to adding numbers.

 

If you have two numbers A and B and you add the with an expression A + B, the results might not be a number.

If A is 3 and B is 5, the result will be the number 8.

If A is 3, but B is null, the result will be null.

If A is null and B is also null, the result will be the String value "nullnull" !!!!!!!!....

And this 3rd case was breaking JR reports due to class cast exceptions, since JR was expecting a number or at least null. But not a String!!!

 

In an unsuccesful attempt in solving this, JR internal Groovy script generator was modified in the hope that forcing a NPE one each of the A and B members in the expression would make JR work exactly like before. Unfortunately, this fix did not take into account expression with the logical operator inside, in which A or B are actually tested for null.

 

I have since reverted the unfortunate fix from JR and tried a different approach. I have now refactored JR so that the type of the expression no longer needs to be declared at report design time. This in turn eliminates all cast operations that JR perfomed on the result of expressions and thus removes the risk of getting a class cast exeption when the Groovy result is not of the type you would expect. This changes are now committed on JR SVN repository and will be part of the JR 4.0.3 version to be released soon.

 

Thank you,

Teodor

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...