Variables and Arithmetic

By: Gary Hill - gary_hill94
Variables and Arithmetic
2003-08-14 20:54
I have two variables of type Double that are sums of two fields of type Double. I would like to assign the difference of these two variables to a third variable, of type double, to print in a group footer. I get an invalid cast error from the report compiler when I do this. Here are the lines from my report definition:

<variable name="balance" class="java.lang.Double" resetType="None" calculation="Nothing">
<variableExpression>$V{gross} - $V{receipts}</variableExpression>
</variable>

Here are the errors:

dori.jasper.engine.JRException: Errors were encountered when compiling report design:
/home/ghill/office/JasperReports/ArDetail.java:232: Invalid cast from int to java.lang.Double.
value = (java.lang.Double)(((java.lang.Double)variable_gross.getValue()) - ((java.lang.Double)variable_receipts.getValue()));
^
/home/ghill/office/JasperReports/ArDetail.java:232: Incompatible type for -. Can't convert java.lang.Double to int.
value = (java.lang.Double)(((java.lang.Double)variable_gross.getValue()) - ((java.lang.Double)variable_receipts.getValue()));
^
/home/ghill/office/JasperReports/ArDetail.java:445: Invalid cast from int to java.lang.Double.
value = (java.lang.Double)(((java.lang.Double)variable_gross.getOldValue()) - ((java.lang.Double)variable_receipts.getOldValue()));
^
/home/ghill/office/JasperReports/ArDetail.java:445: Incompatible type for -. Can't convert java.lang.Double to int.
value = (java.lang.Double)(((java.lang.Double)variable_gross.getOldValue()) - ((java.lang.Double)variable_receipts.getOldValue()));
^
/home/ghill/office/JasperReports/ArDetail.java:658: Invalid cast from int to java.lang.Double.
value = (java.lang.Double)(((java.lang.Double)variable_gross.getEstimatedValue()) - ((java.lang.Double)variable_receipts.getEstimatedValue()));
^
/home/ghill/office/JasperReports/ArDetail.java:658: Incompatible type for -. Can't convert java.lang.Double to int.
value = (java.lang.Double)(((java.lang.Double)variable_gross.getEstimatedValue()) - ((java.lang.Double)variable_receipts.getEstimatedValue()));
^
6 errors





By: Teodor Danciu - teodord
RE: Variables and Arithmetic
2003-08-14 23:47

Hi,

Here's the correct expression :

<variableExpression>
new Double(
$V{gross}.doubleValue() - $V{receipts}.doubleValue()
)
</variableExpression>

You might also consider testing the "gross" and "receipt" variable for null in the expression using the ?: java operator.

I hope this helps.
Teodor
2002 JI Open Discussion's picture
Joined: Aug 10 2006 - 3:28am
Last seen: 17 years 1 month ago

0 Answers:

No answers yet
Feedback