augarte Posted October 27, 2010 Share Posted October 27, 2010 Hi, I've a field "pxf" that is a string but I want to cast to Double. I guess the easiest way for doing that is creating a variable. I have created a variable with the following expression: Double.parseDouble($F{pxf})It seems it is ok but when I execute the report I get the following error: 1. Cannot cast from double to Double value = (java.lang.Double)(Double.parseDouble(((java.lang.String)field_pxf.getValue()))); //$JR_EXPR_ID=8$ <------------------------------------------------------------------------------>2. Cannot cast from double to Double value = (java.lang.Double)(Double.parseDouble(((java.lang.String)field_pxf.getOldValue()))); //$JR_EXPR_ID=8$ <--------------------------------------------------------------------------------->3. Cannot cast from double to Double value = (java.lang.Double)(Double.parseDouble(((java.lang.String)field_pxf.getValue()))); //$JR_EXPR_ID=8$ <------------------------------------------------------------------------------>3 errorsI have also tried changing the expression to this one: new Double (${pxf})But I also have an error with this one: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : Source text : new Double($F{pxf})I have set the variable type to "java.lang.Double" and the type of the TextField where I try to print the variable is also "java.lang.Double". I don't know how can I fix that. Any idea? Thanks in advance, Cheers, Aitor.P.D. I attach the .jrxml file Link to comment Share on other sites More sharing options...
cbarlow3 Posted October 27, 2010 Share Posted October 27, 2010 If I set my scripting language to JavaScript, I'm able to cast the String fields to Double variable using the parseFloat() function. Actually, JavaScript is so forgiving on data types, that I also got it to work with just making the expression be "$F{doublestring}*1" Just to be sure, I also defined a variable that keeps a sum of all the double values and displays in a summary. jrxml pasted below, and screenshot of the report attached. Good luck!Carl Code: Post Edited by cbarlow3 at 10/27/2010 15:59 Link to comment Share on other sites More sharing options...
svenn Posted October 27, 2010 Share Posted October 27, 2010 You are getting that error because parseDouble returns a double (primative) and not a Double which is a object. Though they are both double they are not exactly the same thing.Use the follow which converts a String to a DoubleDouble.valueOf($F{pxf}) Link to comment Share on other sites More sharing options...
augarte Posted October 28, 2010 Author Share Posted October 28, 2010 Thanks for your answers!!! Double.parseDouble ($F{pxf}) is working!!! I only have one problem: I use a xml file as datasource and the is an element which is empty. Casting this element gives me an error because it tries doing the following: Double.valueOf (""). Do you know how can I solve that? Thanks in advance!Cheers,AitorPost Edited by augarte at 10/28/2010 06:22 Link to comment Share on other sites More sharing options...
mdahlman Posted October 28, 2010 Share Posted October 28, 2010 You'll have to decide what you want to do with null values. One possibility is converting them to 0. You could use an expression like this:Double.parseDouble( ($F{pxf}==null) ? 0 : $F{pxf} )If your field is a String you might also need to test for an empty string rather than just for a null object. The same ideas will apply.Regards,Matt 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