I’m pretty new to OH Lambdas but I got it working, but when my variable doesn’t have a value, therefore is null - I’m getting a error in the logs.
to avoid this I wanted to write a simple if statement to check if theyre null or not:
if (gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M"].head.state as Number).intValue !== null)
But I get a error:
2018-11-29 22:44:21.775 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'AlarmClock.rules' has errors, therefore ignoring it: [110,102]: no viable alternative at input '.'
You may need to break down the steps here a bit.
You’re looking for an Item by name within a group, that’s fine.
But there may not be an Item with that name, in other words the filter will return null.
You can’t look at the .state of null, so there’s an error.
You’d need to test if you returned an Item before attempting to get it’s state.
Once you know you have an Item, you can be confident that it has a .state
But that state may be. uninitialized, in which case it is NULL
You can’t take an intValue of NULL, so there’s an error.
You may need to check for a state of NULL before further processing.
The method .head will return the first item of a list of items. It’s the opposite of the .last method. <groupitem>.members.filter will always return an item list, even if the list is empty or of size 1.
var myNumber = (gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].head.state as Number).intValue
First off all big thanks to @Udo_Hartmann, syntax is working so far!
But I’m still getting an error:
2018-12-02 17:25:00.315 [vent.ItemStateChangedEvent] - lR_leE changed from 2018-12-02 17:24:00.317 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Mein Wecker': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_equals(int,int) on instance: null to 2018-12-02 17:25:00.228 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Mein Wecker': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_equals(int,int) on instance: null
Keymessage of this error is:
IntegerExtensions.operator_equals(int,int) on instance: null
So is a integer even able to hold a null value? is a Number item even able to a null value?
Thanks in advantage!
Well… yes, if using a var or a number item and not assigning a value, this var or item.state will stay null (or NULL)
In fact, it’s possible to explicitly assign null to a var myVar = null and it’s also possible to update an item to NULL myItem.postUpdate(NULL)
Hmm, not quite understanding what’s wrong with my code then …
Reason I’ve wanted to check if my lambda is null or not is because I use a number item from my UI.
So basically I want it to assign it a default value, if it’s not configured/set on the User interface to avoid a error message …