thelinuxmaniac Posted October 10, 2006 Share Posted October 10, 2006 Hi,I have three Double fields as: Code:<field name="grandtotal" class="java.lang.Double"/><field name="amount_paid" class="java.lang.Double"/><field name="previous_due" class="java.lang.Double"/> And I want to calculate Balance using the formula:BALANCE_DUE = grandtotal+previous_due-amount_paid When I define a variable to calculate BALANCE_DUE as shown below Code:[code]<variable name="BALANCE_DUE" class="java.lang.Double" resetType="Report" calculation="Nothing"> <variableExpression><![CDATA[sUM( $F{grandtotal} + $F{amount_paid})]]></variableExpression> </variable> I get the following error messageCode:[code]et.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 1. The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double value = (java.lang.Double)(SUM( ((java.lang.Double)field_grandtotal.getValue()) + ((java.lang.Double)field_amount_paid.getValue()))); <------------------------------------------------------------------------------------------------> 2. The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double value = (java.lang.Double)(SUM( ((java.lang.Double)field_grandtotal.getOldValue()) + ((java.lang.Double)field_amount_paid.getOldValue()))); <------------------------------------------------------------------------------------------------------> 3. The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double value = (java.lang.Double)(SUM( ((java.lang.Double)field_grandtotal.getValue()) + ((java.lang.Double)field_amount_paid.getValue()))); <------------------------------------------------------------------------------------------------> 3 errorsWhy cannot + and - operators be applied to Double value (and only applied to strings) ? Please suggest any other possible method to calculate the BALANCE_DUE. Link to comment Share on other sites More sharing options...
rsilverns.sympatico.ca Posted October 10, 2006 Share Posted October 10, 2006 It is related to the underlying functionality of the '+' operator in java. Strings are able to be appended using the '+' even though they are objects, its an exception to the rule. To add the Double objects you have to treat each field as an object and use dot-notation in your expression fro the BALANCE_DUE field. new Double($F{grandtotal}.doubleValue() + $F{previous_due}.doubleValue() - $F{amount_paid}.doubleValue()) Notice that there is no ";" at the end of the expression. Now if one of the above objects is null, a null ptr expression would occur. To avoid this you have to modify the expression as follows... new Double($F{grandtotal}==null?0.0:$F{grandtotal}.doubleValue() + $F{previous_due}==null?0.0:$F{previous_due}.doubleValue()-$F{amount_paid}==null?0.0:$F{amount_paid}.doubleValue()) Now there is a check to use 0.0 if the object is null. This depends on your data source etc. of course, if the object could be null. hth, Robin Link to comment Share on other sites More sharing options...
rcracel Posted October 11, 2006 Share Posted October 11, 2006 Or you can make sure your compiler is compatible with java5 and it should handle the boxing/unboxing for you. Hope this helps,RC Link to comment Share on other sites More sharing options...
thelinuxmaniac Posted October 11, 2006 Author Share Posted October 11, 2006 Thanx Robin for that quick reply. Link to comment Share on other sites More sharing options...
rsilverns.sympatico.ca Posted October 12, 2006 Share Posted October 12, 2006 Yw Post edited by: rsilver@bfm.bm, at: 2006/10/11 19:39 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now