Java Jasper 报告中的表达式值:“无法从字符串转换为布尔值”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1148986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression Value in Jasper Report: "Cannot cast from String to Boolean" error
提问by Ibrahim AKGUN
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.
如果 Personel 获得报酬,她/他的 Jasper 税务报告将显示为PAID
,否则为NO PAID
。在 DB 中,该字段是布尔类型,但表达式返回的是 String 类型。所以我收到一个Cannot cast from String to Boolean
错误。
采纳答案by Ibrahim AKGUN
OK, I fixed it. I changed $F{Personel_ODEME}
's type to String, then it worked like a charm.
好的,我修好了。我将$F{Personel_ODEME}
的类型更改为字符串,然后它就像一个魅力。
回答by akf
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)
,这贾斯帕思想是String
,以Boolean
比较和犯规等。要解决此问题,请尝试以下操作:
($F{Personel_ODEME}.equals(Boolean.TRUE.toString())) ? "PAID" : "NO PAID"
This will result in a String
to String
comparison.
这将导致String
以String
比较。
It is good to note that In Java, a "true".equals(Boolean.TRUE)
would result to false.
值得注意的是,在 Java 中,a"true".equals(Boolean.TRUE)
会导致 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.
这似乎是一个 Jasper 'PrintWhen' 表达式,它允许您确定是否打印单元格的内容。它正在期待Boolean.TRUE
或Boolean.FALSE
作为它的返回值。当您返回“PAID”时,Jasper 会尝试将其评估String
为 a Boolean
,但它不能,因此会引发异常。
回答by ragavan
Try this:
尝试这个:
Boolean.valueOf($F{Personel_ODEME}.equals('TRUE')) ? "PAID" : "NO PAID"
回答by Gallows
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.
您必须将字段的类表达式更改为 java.lang.String,因为默认情况下它将是 java.lang.Boolean。当它评估您的 if 并尝试返回“PAID”或“NO PAID”时,该字段会尝试将其转换为布尔值以便显示。
All credit goes to:
所有功劳归于:
http://jasperforge.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=61775
http://jasperforge.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=61775
回答by Abdur Rahman
($F{Personel_ODEME}==true?"PAID":"NO PAID")
Note: true(keyword) is case sensitive. And should not be capital(TRUE)
注意:true(keyword) 区分大小写。并且不应该是大写的(TRUE)