Jump to content
Changes to the Jaspersoft community edition download ×

$V{MyVariable} == null -> returns null


banania

Recommended Posts

Hello guys,

Well, almost all is said in the title.

I currently have a strange behavior working with iReport ( / jasper reports) 4.0.2. : for some fiels and variables, i try to replace null values with fixed values (most of the time "0"). To do this simple thing, i use a small piece of code in the variable expression :

($F{MyField} == null) ? new Double("0") : $F{MyField}

But i still get null values in the report. Before getting completely nuts, i made a new test with a new boolean variable :

$F{MyField} == null

and, surprise, it returns null. Ok now I understand why this is not working for my variables... but why is this condition not giving true when myField is null ?

 

By the way, "Replace null by blank" option is working, but is not sufficient for my report.

 

Does anyone have an idea ?

 

Regards

Banania

Link to comment
Share on other sites

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

Thanks for your answer.

 

Yes good point, but checked the database and it's really a NULL value. If I check "blank on null" value is blanked.

 

I made a really simple report just to reproduce problem. See below.

Variable "variable1" just check if MT_LIB field is null. It prints 'false' is field is not null... but it print "null" instead of "true" if field is null.

I'm losing my last hairs...

 

<?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 http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" 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 CO_NUMCO, LI_MTLIB
FROM table1
WHERE field1 = 69906]]>
    </queryString>
    <field name="CO_NUMCO" class="java.lang.Integer"/>
    <field name="LI_MTLIB" class="java.lang.Double"/>
    <variable name="variable1" class="java.lang.String">
        <variableExpression><![CDATA[$F{LI_MTLIB}==null]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="24" splitType="Stretch">
            <staticText>
                <reportElement x="36" y="2" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[CO_NUMCO]]></text>
            </staticText>
            <staticText>
                <reportElement x="189" y="2" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[LI_MTLIB]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="19" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.Integer"><![CDATA[$F{CO_NUMCO}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="150" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.Double"><![CDATA[$F{LI_MTLIB}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="289" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$V{variable1}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>
 

Link to comment
Share on other sites

Dear Bana..

 

As per my understanding, you have a field where there is some data on database and another consist of null value.

and you want to print '0' in place of null .....if i m on right path then

 

solution is here...............!

you already had a field associated with database field....now you drawn on static label exactly above the field where you want to print '0' in case value is null then make it hide by uncheck on opaque so that it should not display normally.....!....now use print when expression properties of jasper on that field and put condition like new Boolean(field name(having value from database =="" or having value from database==null) and on acutal field use print on expression with condition new Boolean(field name having value from database!=null)

Hope this will work!......

Link to comment
Share on other sites

This won't address the underlying issue with your report design but will give you the results you want: force the database to always return a value in that field.

eg. SELECT field1, field2, nvl(problemfield,0) as problemfield FROM table.....

Note: The equivalent to Oracle's nvl() in MySQL is ifnull(), in postgres and db2 it's coalesce(), in MS-SQL it's isnull()

Link to comment
Share on other sites

  • 4 months later...

 Hi,

One point, are you using "Groovy" for the Report Language ?

If so, try changing it to "Java".

I have had similar problems trying to handle null Integer parameters; in Groovy, you can just say

$F(field1) ? $F(field1) : "Default Value"

If something is null its effectively  FALSE.

However, I have only manage to get this to work with Strings.

Steve

Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

It's 3 years later and I'm sure the original poster does not remember the solution, but I'm having the exact same problem.

I have a variable that has the result of a scriplet method which returns "null". It's not a string, and it doesn't seem like an object eitherbecause I can't compare it to anything.. It is just null, plain and simple.

How do I get around this problem? I need to know if it is null to be able to "ignore" it's value.

This is what I wanted:

($V{IMAGE_PATH} != null ) ? $V{IMAGE_PATH} : "" 

Would greatly appreciate some input =)

Cheers from Spain!

Link to comment
Share on other sites

  • 3 years later...

Hi What I did is the following: In the sql statement I'm working with postgres check syntax to another database. 

I added :

 select field1,field2, case when (contract.date_end IS NULL) THEN 0 ELSE 1 END as check,

So I have a field called check

And I used in an expression as an IF

$F{check}==0 ? " A text according to the definition" : " to " + $F{fechaf}.toString()

I hope this will help someone. 

The other solution could be create a class and added to the report as a jar file. 

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...