Variable expression

Hi,
I want to count the number of row where a fields is equals ="hello". I did a new variabile (type counter) and put in a variable expression something like this:

$F(nameField).equals("hello")
but it doesn't work: CannotÂcastÂfromÂbooleanÂtoÂObject

What's the problem??
daniele's picture
381
Joined: Jan 12 2007 - 1:45am
Last seen: 16 years 8 months ago

2 Answers:

There was an excellent post on this just a couple of days ago, but I can't find it.


The crux of the problem is that your comparison results in a primitive boolean result but your variable is defined as an Integer object.


What you need to put in your variable's expression is something like this:


$F{ProtocolName}.compareTo("hello")==0 ? new java.lang.Integer(1) : null


If you wanted to use a Sum variable instead of a counter then your variable's expression would be


$F{ProtocolName}.compareTo("hello")==0 ? new java.lang.Integer(1) : new java.lang.Integer(0)
jmurray's picture
7499
Joined: Dec 11 2006 - 11:19am
Last seen: 16 years 9 months ago
You should buy the documentation, it's worth it - saved me a lot of trouble.

Some more info on count: Count will incriment when it reaches any non-null data, therefore you have to use a single line expression to say that if it's NOT equal to your comparison, consider it null.

I like using methods that return a boolean, but depending on what JRE you're using, you'll have to choose your own method. If you're returning a boolean on count simply say:

$F{ProtocolName}.equals("hello") ? "count me" : null

where count me could be literally anything.

On the contrary, if you want to count everything that DOES NOT meet that criteria, simply change it to:

$F{ProtocolName}.equals("hello") ? null : "count me"
schaibaa's picture
450
Joined: Jan 8 2007 - 1:04am
Last seen: 16 years 8 months ago
Feedback