Problems casting from string to Double

0

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 errors

I 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

augarte's picture
531
Joined: Jan 27 2010 - 7:20am
Last seen: 1 year 5 months ago

4 Answers:

0

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:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports <a target="_blank" href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd""><a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a></a> name="report6" language="javascript" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<queryString>
		<![CDATA[SELECT
     mytable.`doublestring` AS doublestring
FROM
     `mytable` mytable]]>
	</queryString>
	<field name="doublestring" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<variable name="double" class="java.lang.Double">
		<variableExpression><![CDATA[parseFloat($F{doublestring})]]></variableExpression>
	</variable>
	<variable name="doubleSum" class="java.lang.Double" calculation="Sum">
		<variableExpression><![CDATA[$V{double}]]></variableExpression>
	</variable>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="29" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="275" height="29"/>
				<textElement>
					<font size="12" isBold="true"/>
				</textElement>
				<text><![CDATA[Sample JavaScript cast of string to Double]]></text>
			</staticText>
		</band>
	</title>
	<columnHeader>
		<band height="20" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="100" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[String Version]]></text>
			</staticText>
			<staticText>
				<reportElement x="142" y="0" width="100" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[Double Version]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="20" splitType="Stretch">
			<textField>
				<reportElement x="0" y="0" width="100" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{doublestring}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="142" y="0" width="100" height="20"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression class="java.lang.Double"><![CDATA[$V{double}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<summary>
		<band height="20" splitType="Stretch">
			<textField>
				<reportElement x="142" y="0" width="100" height="20"/>
				<textElement textAlignment="Right">
					<font isBold="true"/>
				</textElement>
				<textFieldExpression class="java.lang.Double"><![CDATA[$V{doubleSum}]]></textFieldExpression>
			</textField>
		</band>
	</summary>
</jasperReport>


Post Edited by cbarlow3 at 10/27/2010 15:59
cbarlow3's picture
1420
Joined: Mar 4 2010 - 8:59am
Last seen: 5 days 10 hours ago
0

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 Double

Double.valueOf($F{pxf})

svenn's picture
1365
Joined: Mar 19 2007 - 5:57am
Last seen: 12 years 6 months ago
0

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,

Aitor



Post Edited by augarte at 10/28/2010 06:22
augarte's picture
531
Joined: Jan 27 2010 - 7:20am
Last seen: 1 year 5 months ago
0

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

 

mdahlman's picture
9910
Joined: Mar 13 2007 - 2:43am
Last seen: 4 years 10 months ago
Feedback