type casting of numeric fields

I don't know why Java is so hard when it comes to type casting. I have few numeric fields of type BigDecimal. When I try to add them together and if one of them is null, the whole total also shows as null. So. I try to evaluate each field before addition like this:

$F{QTY1}.equals(null)?0:$F{QTY1}

The error I get is:

Incompatible conditional operand types int and BigDecimal

I even tried using:

$F{QTY1}.intValue().equals(null)?0:$F{QTY1}

but got error: Cannot invoke equals(null) on the primitive type int

Any ideas to save my frustration?



Post Edited by dreporter at 09/02/2009 15:52
dprogrammer's picture
Joined: Jul 19 2006 - 7:37am
Last seen: 2 months 1 day ago

5 Answers:

Try using Groovy as report language.  I don't think it will add null BigDecimals, but it will simplify your expressions.

Regards,

Lucian

lucianc's picture
87189
Joined: Jul 17 2006 - 1:10am
Last seen: 22 hours 22 min ago

I did that. I changed Default Expression language to Groovy. I stil don't see the whole total correctly. It appears as null. If I have to apply casting again even after use of Groovy then what is the advantage of using it?

dprogrammer's picture
Joined: Jul 19 2006 - 7:37am
Last seen: 2 months 1 day ago

try:

$F{QTY1}==null?new BigDecimal(0):$F{QTY1}

regards

/Markus

 

zeltner's picture
274
Joined: Jan 22 2007 - 4:48pm
Last seen: 3 months 20 hours ago

dreporter
Wrote:

I did that. I changed Default Expression language to Groovy. I stil don't see the whole total correctly. It appears as null. If I have to apply casting again even after use of Groovy then what is the advantage of using it?

I said that I didn't think that will allow you to add null BigDecimals.

Groovy would help with the "Incompatibleconditionaloperandtypes" error and in general allow you to write simpler expressions (no intValue() or new Integer(..) required).

Regards,

Lucian

lucianc's picture
87189
Joined: Jul 17 2006 - 1:10am
Last seen: 22 hours 22 min ago

 Hi,

I am trying to use the tertiary operator technique for a Text Field expression on an Integer parameter and field,
but the result is always null, if the parameter or field is empty.

e.g "$P{integer1} ? new Integer(1) : new Integer(0)"

In a simple report (jrxml is below), using the Empty datasource, I have 2 fields (a String and an Integer)
and 2 parameters  (also a String and an Integer).

When previewed, the String Field and Parameter show "FALSE", but the Integer Field and Parameter shows null.

If the Report language is changed to Java, and the expressions modified to 

"$P{integer1} != null ? new Integer(1) : new Integer(0)"

then the correct values are shown.

Is this actuallly a Groovy issue, or is there something in JasperReports that is causing this ?

Thanks

 

 

 

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 href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a> name="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.5"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<parameter name="integer1" class="java.lang.Integer" isForPrompting="false"/>
	<parameter name="string1" class="java.lang.String" isForPrompting="false"/>
	<field name="field1" class="java.lang.String"/>
	<field name="field2" class="java.math.BigDecimal"/>
	<title>
		<band height="56" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="21" width="555" height="20"/>
				<textElement textAlignment="Center">
					<font size="14"/>
				</textElement>
				<text><![CDATA[Test Report for null Parameters and Fields]]></text>
			</staticText>
		</band>
	</title>
	<detail>
		<band height="132" splitType="Stretch">
			<textField>
				<reportElement x="17" y="14" width="419" height="20"/>
				<textElement>
					<font size="14"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{field1} ? "TRUE" : "FALSE"]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="17" y="92" width="419" height="20"/>
				<textElement>
					<font size="14"/>
				</textElement>
				<textFieldExpression class="java.lang.Integer"><![CDATA[$P{integer1} ? new Integer(1) : new Integer(0)]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="17" y="72" width="419" height="20"/>
				<textElement>
					<font size="14"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$P{string1} ? "TRUE" : "FALSE"]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="17" y="32" width="419" height="20"/>
				<textElement>
					<font size="14"/>
				</textElement>
				<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{field2}  ? new Integer(1) : new Integer(0)]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport></td></tr></tbody></table>
stevep0000's picture
Joined: Sep 30 2010 - 6:56am
Last seen: 2 months 3 weeks ago
Feedback
randomness