Why do I have error in the expression editor when I sum BigDecimal in Jaspersoft Studio? - jasper-reports

I want to sum two BigDecimal, but before to do that I need to know that this values are not null (I could made this check Java side, but I want to try Jasper side).
So, I do this check:
($F{a} != null ? $F{a} : new BigDecimal(0))
($F{b} != null ? $F{b} : new BigDecimal(0))
Now, I have to sum a and b, but if I do:
($F{a} != null ? $F{a} : new BigDecimal(0)).add($F{b} != null ? $F{b} : new BigDecimal(0))
I get:
The current expression is not valid. Please verify it!
How can I solve?

Assuming that your fields are both of class java.math.BigDecimal and that you are using language="java" or language="groovy"
There is nothing wrong with your expression!
Don't worry about the Expression Editor it simple can not understand that ($F{a} != null ? $F{a} : new BigDecimal(0)) is a java.math.BigDecimal, that is why it states "The current expression is not valid. Please verify it!" and show's a red dot at .add.
Just press "OK" and enjoy your result!
If you like to simplify your expression's or just remove the error in the expression editor, you can use variables.

I had the same problem. Explicitly casting a new BigDecimal eliminates the exception. Use
new Bigdecimal($F{a} != null ? $F{a} : new BigDecimal(0))
and there should be no problem.
I was wrong. The cast eliminated Jaspers' compile time exception but in runtime it threw 'The constructor BigDecimal(NumberComparable) is undefined.' Note the compile time exception is bad since their approach is to check each keystroke and throw up a warning as you type. In order to complete typing the conditional, it takes double the effort in warnings closing. Now I need my code to work without the cast and I'm about to try
($F{denominator} == 0 ? 0 : $F{numerator} / $F{denominator})

Related

AnyLogic inject agents from existing population with characteristics

I created a population with objects from a database with individual characteristics.
I am trying that the objects with a specific value are injected into a source for process-modelling.
("ankunftszeit" means "arrival time")
for (mp_lkw l : pop_mp_lkw){
if (l.ankunftszeit == getTime()){
source_mp_lkw.inject(mp_lkw l, 1, false, false);
}
}
But somehow an Error occurs and I cannot find any solution...
It says taht the inject() only accepts integers
The method inject(int) in the type Source<mp_lkw> is not applicable for the arguments (mp_lkw, int, boolean, boolean)
Syntax error on token "l", delete this token
Where is my mistake and how can it be corrected?
Not sure I understand your issue, but if the agents already exist, you shouldn't use a source, instead use an enter block, and then change your code to (even though this code seems like a terrible idea... instead you should use dynamic events but I'm not sure your purpose)
for (mp_lkw l : pop_mp_lkw){
if (l.ankunftszeit == getTime()){
enter.take(l);
}
}

Dart null safety !. vs ?-

what is the difference exactly between
String id = folderInfo!.first.id; //this works
and
String id = folderInfo?.first.id; //this is an error
I know ?. returns null when the value object is null but what does the !. return?
?. is known as Conditional member access
the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)
In your case, String id means id can not have null value. But using ?. can return null that's why it is showing errors.
!. is use If you know that an expression never evaluates to null.
For example, a variable of type int? Might be an integer, or it might be null. If you know that an expression never evaluates to null but Dart disagrees, you can add ! to assert that it isn’t null (and to throw an exception if it is).
More and ref:
important-concepts of null-safety and operators.
The Assertion Operator (!)
Use the null assertion operator ( ! ) to make Dart treat a nullable expression as non-nullable if you’re certain it isn’t null.
In other words !. will throw an error if the value is null and will break your function and as you know ?. will return null with no interruption.
The ! throws an error if the variable is null. You should try to avoid this if possible.
If you’re sure that an expression with a nullable type isn’t null, you can use a null assertion operator (!) to make Dart treat it as non-nullable. By adding ! just after the expression, you tell Dart that the value won’t be null, and that it’s safe to assign it to a non-nullable variable.
In your first case, you define id not nullable but when you set nullable value then throw error.
String id = folderInfo?.first.id;
In 2nd case, when you use assertion operator (!), it actually tell compiler that it must be non nullable.

dart null checking: why do i have to use a null check operator ('!') after checking already for null ( != null ) [duplicate]

This question already has answers here:
"The operator can’t be unconditionally invoked because the receiver can be null" error after migrating to Dart null-safety
(3 answers)
Closed 1 year ago.
I updated my flutter project to nullsafety and get errors saying:
The function can't be unconditionally invoked because it can be 'null'.
Try adding a null check ('!').
For a variable that I already checked for null. What is the correct way to do this now? I know I can just add ! because i know it can't be null. But I don't see why. If there was more code it could happen, that the null check is deleted and the ! operator stays.
Here is my code example:
final Function? suggestionsCallback;
if (widget.suggestionsCallback != null &&
widget.defaultSearchPattern.isNotEmpty &&
pattern.isEmpty) {
return await widget.suggestionsCallback(widget.defaultSearchPattern); // here is the error
} else {
return widget.suggestionsCallback != null
? await widget.suggestionsCallback(pattern) // here is the error
: [];
}
Take a quick look at my code:
class Hello{
final Function? hello;
Hello(this.hello);
}
class Say{
wow(){
var h1 = Hello(null);
if(h1.hello!=null) h1.hello();
}
}
https://dart.dev/tools/non-promotion-reasons#property-or-this
Note: "Promotion" here means "determine that a nullable is in fact not null at this line of code";
Dart compiler is not smart enough to infer that your function, after the if-statement, is NOTNULL at that position of your code. It can only tell a local variable is not null after certain condition statements.
You know the variable can’t be null because you just checked. The compiler doesn’t. So unfortunately you have to tell it again.
Actually your specific compiler might be clever enough, but not all compilers are. And whether you have compile time errors shouldn’t depend on how clever the compiler is. Therefore the language will require the ! test. It’s quite possible that the compiler produced no actual test for the !

Flutter null-safety conditionals in object methods

I'm just working through this whole null-safety mode with my Flutter project and unsure what the difference is with ? and ! in calls to object methods.
For example, the hint was to add a ! conditional. Here's an example I have right now, and I'm unsure if this should be a ? or a ! at the findNbr!.replaceAll().
Future checkItem({String? findNbr}) async {
int? x = int.tryParse(findNbr!.replaceAll('-', ''));
...
Does this mean replaceAll() will not run if findNbr is null?
Or should it be a ? instead? findNbr?.replaceAll()
EDIT: I just noticed I cannot use findNbr?, it's telling String? can't be assigned parameter String.
Or does it mean I say it's not null and run it anyway?
For your information, I have not come close to running my app yet so I have no idea if it even works. But I figure I better know what it's doing before get too much more done. I'm still in the process of converting everything and there's 75-100 dart files. I'm not sure I get the point of it all to be honest, because I just add ? to everything, so its all nullable anyway.
Future checkItem({String? findNbr}) async {
int? x = int.tryParse(findNbr!.replaceAll('-', ''));
...
Does this mean replaceAll() will not run if findNbr is null?
Correct. If findNbr is null, then findNbr! will throw a runtime exception. That would be bad, especially since checkItem's function signature advertises that findNbr is allowed to be null, and therefore it would violate callers' expectations.
Or should it be a ? instead? findNbr?.replaceAll()
EDIT: I just noticed I cannot use findNbr?, it's telling String? can't be assigned parameter String.
You can't use findNbr?.replaceAll(...) because if findNbr is null, then it would be invoking int.tryParse(null), but int.tryParse is not allowed to take a null argument.
What you need to do is one of:
Make findNbr no longer optional:
Future checkItem({required String findNbr}) async {
int? x = int.tryParse(findNbr.replaceAll('-', ''));
...
Allow findNbr to be optional but have a non-null default value:
Future checkItem({String findNbr = ''}) async {
int? x = int.tryParse(findNbr.replaceAll('-', ''));
...
Allow findNbr to be optional but explicitly decide what to do if it is null. For example:
Future checkItem({String? findNbr}) async {
int? x = findNbr == null ? null : int.tryParse(findNbr.replaceAll('-', ''));
...
I'm not sure I get the point of it all to be honest, because I just add ? to everything, so its all nullable anyway.
If you blindly add ? to all types and add ! to all variables, then yes, null-safety would be pointless: doing that would give you the same behavior as Dart before null-safety.
The point of null-safety is to prevent things that shouldn't be null from ever being null. You could have written such code before, but without null-safety, that meant performing runtime null checks (e.g. assert(x != null);, if (x != null) { ... }, or relying on a null-pointer-exception to crash the program if null was used where it wasn't expected). Null-safety means that such checks now can be done at build-time by static analysis, which means that errors can be caught earlier and more completely. Furthermore, whereas previously functions needed to explicitly document whether arguments and return values were allowed to be null (and inadequate or incorrect documentation could be a source of errors), now they're self-documenting in that regard. It's just like using int foo(String s) versus dynamic foo(dynamic s); using strong types catches errors earlier and better describes the function's contract.
I recommend reading Understanding Null Safety if you haven't already done so.
I would like to advice you to use the ! operator, also the called bang operator, as little as possible. You should only use this operator when the dart analyser is wrong and you know for 100% that the value will never be null.
Below is an example of where the dart analyser would be wrong and you should use the bang operator.
// We have a class dog with a nullable name.
class Dog {
String? name;
Dog({this.name});
}
void main() {
// We create a dog without a name.
final dog = Dog();
// We assign the dog a name.
dog.name = 'George';
// The dart analyser will show an error because it can't know if the
// name of the object is not null.
//
// Will throw: `A value of type 'String?' can't be assigned to a
// variable of type 'String'`.
String myDogsName = dog.name;
// To avoid this, you should use the bang operator because you `know` it
// is not null.
String myDogsName = dog.name!;
}
The ? operator simply tells Dart that the value can be null. So every time you want to place a ? operator, ask yourself, can this value ever be null?
The null safety features in Dart are mainly created for helping the developer remember when a value can be null. Dart will now simply tell you when you made a variable nullable in order to force null checks or default values for example.

Error when using conditional breakpoint with instanceof.Is it me or eclipse?

My condition for break :
event instanceof org.geomajas.gwt.client.widget.event.SearchEvent
I have tried other variations like event instanceof SearchEvent / with parantheses and with/out ";"
The error : Evaluations must contain either an expression or a block of well-formed statements
The solution: ?
BTW I'm using jdk 1.6.25
return event instanceof org.geomajas.gwt.client.widget.event.SearchEvent;
should do the trick