[Jaspersoft Studio] Date Expression always null

1

Hello, I feel like I'm pulling teeth on the expressions for Jaspersoft. 

I'm trying to get a date to show on my report (ANY date), but when I preview it always shows null!

Here is an example of my expression.

DATEFORMAT(TODAY( ),"MMMMyyyy" )

 

jadonmhale's picture
Joined: Jun 18 2014 - 10:03am
Last seen: 6 years 1 month ago

4 Answers:

0

I know how you feel lolz!

First off, I've never used the dateformat so let's scrap that part.

That leaves us with your TextField with the expression of TODAY( )

Click on the TextField and look at the properties window

there is a section on there called Pattern this is where you will format the date. you can type MMMMyyyy directly in the field since that should work but they also have some built in templates that you can check out too. To see the templates, there is a button to the right of the pattern field with ... on it

Hope this helps ya out and feel free to ask any follow up questions!

 

idstewart's picture
Joined: May 30 2014 - 4:05am
Last seen: 4 years 11 months ago

Thanks for the reply. The Pattern feature reduced my original expression (the one posted was just a test for this null issue).

I still get null on preview though.

This is what I did:
I created a variable.
In the variable I added my expression.
Then I created a text object in my group header.
I then added the variable into the text's expression.
I then previewed the document and I get 'Null'.
There shouldn't be a database issue with the above expression since it should take the date object from the computer's clock.

jadonmhale - 6 years 1 month ago

Jasper is picky, as I have come to learn. As soon as you said the Group Header, I know that was the issue. I'm no guru at Jasper, but I can tell you that Variables don't get calculated until the detail band (from what I have seen)
so you can do one of two things in this instance:
1. use a parameter instead of a variable. It can be used more than just for the user to enter a value.
2. change the evaluation time for the text field. The default evaluation time is Now. So in your case when when it got to the TextField, the value was still null. Thus for the group band, you can switch the eval time to Group[nameofthegroup]

but do not use Auto. From what I have read, it is not worth it and ends up using more resources

idstewart - 6 years 1 month ago

I tried every evaluation time and the result is always the same....null.

I'm trying this expression as I found it is the cause of my null problem but I can't find out why (part of my original expression, the previous one was only a test).
MONTH(IF($F{YearMonth} % 12 == 0, 12, $F{YearMonth} % 12))
The value of YearMonth is 24172

The REST operator works as I tested it without MONTH(OBJ) (returns 4). I need month to work, because the IF statement returns an object and I need it into an integer for the DATE(INT,INT,INT) expression.

jadonmhale - 6 years 1 month ago
0

Check if your variable's type is set to Date.  Text fields are String so try something like new SimpleDateFormat("yyyy/MM/dd").format($V{Variable_1})

 

    <variable name="Variable_1" class="java.util.Date">
        <initialValueExpression><![CDATA[new java.util.Date()]]></initialValueExpression>
    </variable>

 

            <textField pattern="">
                <reportElement x="340" y="49" width="100" height="20" uuid="e394a1cf-e490-4dc4-a528-b95596ee2479"/>
                <textFieldExpression><![CDATA[new SimpleDateFormat("yyyy/MM/dd").format($V{Variable_1})]]></textFieldExpression>
                <patternExpression><![CDATA[new SimpleDateFormat("yyyy/MM/dd").format($V{Variable_1})]]></patternExpression>
            </textField>

 

hozawa's picture
64188
Joined: Apr 24 2010 - 4:31pm
Last seen: 1 year 1 month ago
0

Hi,

To achieve this simply, 

1. You can set the expression of the Text feild where you are showing the Date as   -   new Date()

2. Now you see the properties for that particular text feild , there is a property called Pattern  

3. Click on the small button in the feild you will see a popup which would allow you to set various patterns for Percentage/Number/Date/Currency/Time .. etc 

4. You can then select Date and choose the format, MM/dd/yyyy

Now if  you run the report then you  can see the current date in desired format.

 

Cheers!!

 

Thanks,
Ankur Gupta

http://ankurthetechie.blogspot.in/
 

Ankur Gupta's picture
Joined: Jan 21 2013 - 10:36pm
Last seen: 3 months 3 weeks ago

No change, still says null.
An update. This expression is no longer in my group 1 but now I decided to place it in the page header.

This is my expression:
new Date($F{YearMonth} / 12, MONTH(IF($F{YearMonth} % 12 == 0, 12, $F{YearMonth} % 12)), 1)

(weird how it will error if Date is set to DATE, when rest of the variables are in caps?)

jadonmhale - 6 years 1 month ago

Hi ,

Can you please tell in a example what you are trying to achieve through this expression?

Thanks.

Ankur Gupta - 6 years 1 month ago

Date in my table comes from a single integer, example: 24172
To get the year: the data value is divided by 12 (no decimal).
To get the month: the logic statement mods (or rest) the data value by 12. If the result is 0 then the returned logic value is 12, but if the result is not 0, the returned logic value is the data value mod (or rest) by 12 again.
For the day it is just set to 1.

I need this to be presented at the header of my first group.
The REST mathematical function works in Jasper as tested. but when it is added to the MONTH() it is where I think my problem is, as it just returns null (tested this by just a month expression).

Sorry for the confusion.

jadonmhale - 6 years 1 month ago
0

Try to change Evaluation Time in text field properties.

If you use the variable, it can be that it's not evaluated at the time of displaying it.

From the tutorial "Variables and Expressions" (accesible from Help menu):

Evaluation Time.  The evaluation time is not an attribute of the variable but of the elements that can use variables in their expressions (like a Text Field) and define the "time" when the value of the variable should be read. A variable can potentially change value at every iteration, so a value read at one time may be different from the one obtained during another time. So for every element that can define an expression is possible to say when evaluate it. And since in the expression could be defined one or more variables this parameter also influences also when these variables are read. The possible values are:

Report: the value of the expression is evaluated ad the end of the report.

Page: the value of the expression is evaluated at the end of every page of the report.

Column: the value of the expression is evaluated at the end of each column (if the report is composed only of a column this is equivalent to Page).

Group : the value of the expression is evaluated after the break of the specified group. This option is visible only if at least one group is defined.

Band: the value of the expression is evaluated after the end of the band where the element with this evaluation time is placed. This is a very particular case, introduced to wait that the other elements in the band are completely created. The typical value of the variables are read at the start of the band, but for example suppose to have a subreport with an output parameter to print in the main report. To print this parameter it must be read when the subreport was already computed, so the value could be printed when the band is completely created, in this cases the Band evaluation time is necessary.

Auto: this is used when in the expression of the element there are more variables and fields, that need to be evaluated at different times. The variables are evaluated at a time corresponding to their Reset Type (see below for more information), instead the fields are always evaluated at time -now. This type is useful when report elements have expressions that combine values evaluated at different times (e.g. percentage out of a total).

Now: the value of the expression is evaluated after the read of every record, so at every iteration, this is the default behavior.

kreska07's picture
-11
Joined: Jan 17 2020 - 2:46am
Last seen: 4 months 1 week ago
Feedback