Variable returns null at first row in report - jasper-reports

I am using Jaspersoft Studio for building reports.
I have a variable which actually checks a column TODAY has null values of not. Expression of that value is
$F{TODAY} == null ? new BigDecimal(0.00) : $F{TODAY}
TODAY column has null values in my case for now.
The problem is first the expression above returns null for the first row and zero for the other rows. It should return 0 for every row since TODAY column is null
What can be the reason for that?

The problem is leaving "initial value expression" part empty.
I added the same expression also to that field and it works fine now.

Same problem here. The older fix of use same expression for Initial Value does not work for me, because the first two rows now have same value.
I end up fix it by on iReport, changing the Variable:Property:Reset Type: to None.

i had a similar problem and i fixed it by setting 'evaluation time' of the 'Text Field' showing the variable to 'Auto' instead of 'Now' in jasperSoft Studio

Related

Crystal Report- Set value as Zero (formula)

if database field value null, the crystal report should preview the value as Zero.
Ex: One of the Database.Feildname value null.. When crystal report preview nothing..So i want to set it value as Zero (0)..
Thank you..
That's an indication your field is not a number. So change to:
IF IsNull({yourField}) Then "0" ELSE {yourField}
Crystal has an option to set NULL values to default but I think that's not a good practice.
Instead, use a formula such as: IF IsNull({yourField}) Then 0 ELSE {yourField}

Crystal report conditional section suppress

I've made a crystal report which has a section with name "detail d", and would like to suppress this section if the field "stock_gift" is empty (i.e. "")
I've set the condition in the section expert using below condition:
(trim({gift.stock_gift}) = "" or isnull({gift.stock_gift}))
But the section isn't suppressed, I checked the database whether the field is an empty string and it shows it's an empty string:
Is there anything wrong with the formula that caused the section unable to suppress?
Are you sure that {gift.stock_gift} contains the empty string and NOT a null value? If it's a null then your suppression formula won't work correctly as it is; you need to either swap the order of your boolean statements like this:
isnull({gift.stock_gift}) or trim({gift.stock_gift}) = ""
Or get rid of everything except for the isnull() check. The reason for this is that if CR encounters a null value in a formula and it's not handled via isnull() as the very first thing in the formula, then that formula will produce an error exception and will not continue to be evaluated.

Is there a difference between Null, (Null) and Nothing in SSRS?

My apologies if this is a very easy question.
So In SSRS you can choose either (Null) from a drop-down , or Null, or use an expression with VBs Nothing keyword.
I created a test case. Create a parameter called "TestParam" with three possible values:
(Null) (The prefilled available value)
An expression set to =nothing
Typing "Null" into the value box (as shown in the screen shot.)
The parameter must be set to allow null values to save the first of these.
Then created a dataset:
SELECT
"Valid Row Returned"
WHERE
#TestParam IS NULL
This returned a row if the parameter was set to the prefilled (Null) or =nothing. It did not return a row if the parameter was set to "Null" as shown in the screenshot.

iReport subreport return value

I am using iReport 4.0.2 and I want to show a result in my main report. For example, in my main report we have two columns and I want to get the sum of that two columns, just like this format:
A B sum
10 5 15
Where A is one field in the main report and B is a return value of my subreport.
This works well. But, the key point is that sometimes subreport will not return any value, which is the problem. In this case, the result of sum is like this:
A B sum
10 NULL
As we see here, B is the subreport return value but it's value neither NULL nor 0 . That's why we have that problem.
I try to find how can I get the return value from subreport when the SQL returns no results. I know iReport has a property named 'When No Data', but it doesn't help.
So I want to know, whether we have another way to solve the problem in iReport or using some SQL skills.
I think that in your second example B is actually null but perhaps your text field has the 'Blank When Null' property set. I was able to reproduce your results except for the blank for B.
The real issue is that you can't add or subtract with a null value. JasperReports will just print null. You have to add some logic to provide a default value if one of the variables in the expression is null. For example
$F{MAIN_REPORT_FIELD} - ($V{SUB_RESULT} == null ? 0 : $V{SUB_RESULT})
Unfortunately, this is broken for reports that use Groovy for expressions in JasperReports 4.0.2 (Case 0005138) and will always return null.
You have a few options:
You could upgrade to JasperReports/iReport 4.1.1 which resolves this issue.
You could switch to using Java for expressions.
Depending on your report this will either be painless or it will be a chore. Any fields that don't have the correct Expression Class will have to be fixed up and you'll probably have to be more explicit in any expression that performs operations on non primitive/wrapper types.
if you are doing sum of main field and sub report field in main report then in the sum expression (main report field + (subreportfield==null ? 0 : subreportfield) .
return subreport values to main report variable.
Then do the sum of the main report field and bareport return variable.
Do the same condition as given above.

JasperReports: default value instead of 'null'

Is there any way to set a default value to a field in a report? I have a lot of String fields in a report and would like them to display "0,00" when they're null.
Supposing the field name is "value", in the "Text Field Expression", write:
($F{value} != null) ? $F{value} : "0.00"
You can also select "Blank when null" in the properties of the text field if you want that. Other options are more flexible but this does the trick very quick and easy.
medopal's answer is good, but 2 additions:
1) You can make the syntax shorter:
($F{field_name}) ? $F{field_name} : "0.00"
2) Make sure your "else" data is of the same class as the field's value, otherwise you'll get errors when it tries to coerce numbers into string, etc. etc. This was something that, as I started out, I mixed up.
Did you try set a pattern in the text field?
If you are using iReport this can be found in the properties for the text field in the Text Field Properties section.
Try something along the lines of ###0.00 to represent 1234.56, which would always display 0.00 even if it is null.
This is the easiest way is to use Coalesce() or NVL() function of database in your data-source query to restrict null data on your report.
But it depends on if you are allowed to change the datasource query or not. If not then you can go for other solutions provided in previous answers.