Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression Value in Jasper Report: "Cannot cast from String to Boolean" error

This is my expression code:

($F{Personel_ODEME}.equals(Boolean.TRUE)) ? "PAID" : "NO PAID"

If Personel is paid, her/his Jasper tax report will read PAID, otherwise NO PAID. In the DB, this field is Boolean type, but the expression is returning a String type. So I am getting a Cannot cast from String to Boolean error.

like image 933
Ibrahim AKGUN Avatar asked Jul 19 '09 00:07

Ibrahim AKGUN


4 Answers

The problem stems from your test $F{Personel_ODEME}.equals(Boolean.TRUE), which Jasper is thinking is a String to Boolean comparison, and doesnt like. To fix this, try this:

($F{Personel_ODEME}.equals(Boolean.TRUE.toString())) ? "PAID" : "NO PAID"

This will result in a String to String comparison.

It is good to note that In Java, a "true".equals(Boolean.TRUE) would result to false.

edit:

This appears to be a Jasper 'PrintWhen' expression, which allows you to determine whether to print the contents of a cell or not. It is expecting Boolean.TRUE or Boolean.FALSE as its return values. When you return "PAID", Jasper tries to evaluate that String as a Boolean, which it cant, so it throws the exception.

like image 192
akf Avatar answered Sep 27 '22 21:09

akf


OK, I fixed it. I changed $F{Personel_ODEME}'s type to String, then it worked like a charm.

like image 26
Ibrahim AKGUN Avatar answered Sep 27 '22 22:09

Ibrahim AKGUN


You have to change the Class Expression for the field to java.lang.String as it will be java.lang.Boolean by default. When it evaluates your if and tries to return "PAID" or "NO PAID", the field tries to convert that to Boolean so it can be displayed.

All credit goes to:

http://jasperforge.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=61775

like image 36
Gallows Avatar answered Sep 27 '22 21:09

Gallows


Try this:

Boolean.valueOf($F{Personel_ODEME}.equals('TRUE')) ? "PAID" : "NO PAID" 
like image 40
ragavan Avatar answered Sep 27 '22 22:09

ragavan