[SOLVED] Check if variable inside a lambda is null

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 '.'

.head?

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.

Maybe

var myNumber = (gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].head.state as Number).intValue

will suffice. Or use two steps:

if(gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].size > 0)
    var myNumber = (gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].head.state as Number).intValue
1 Like

Thanks. I did not know.
My reply did not mean to appear terse, I was on the phone :slight_smile:

Never mind :slight_smile:

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 …

if(gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].size > 0)
{
sollMinute = (gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M"].head.state as Number).intValue
}else{sollMinute = 9}

What’s .size after the lambda doing?

The filter on the group returns a list of members. .size operates on the list, so you have a test to to see if the list is zero length i.e. no members met the filter criteria.

Personally I’d structure this a bit different. Do the name match list and get the .head (how many could there be?)
Now test that for no-match null before the rest.

Well, your test is: “Is there any member in group gPERSON1Wecker which is not NULL?” and then you use this group without testing for NULLness.

The result of .filter[] is a list of items. .size is the count of items in this list. .head is the first item of the list.

I would do it this way:

sollMinute = 9
if(gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].size > 0) sollMinute = (gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL].head.state as Number).intValue
//                                                                                                        ^^^^^^^^^^^^^^^^^^ check for NULL here                                                 ^^^^^^^^^^^^^^^^^^ and also check for NULL here

a little enhancement:


var myList = gPERSON1Wecker.members.filter[s | s.name == "PERSON1_WECKER_"+Heute+"_M" && s.state != NULL]
//                                                                                   ^^^^^^^^^^^^^^^^^^ check for NULL here                                                 
sollMinute = 9
if(myList.size > 0) sollMinute = (myList.head.state as Number).intValue
1 Like

Another point would be the s.name

I guess :wink: all members names of gPERSON1Wecker start with PERSON1_WECKER_ ? You could use
s.name.endsWith(Heute+"_M") instead.

1 Like

Thanks alot udo :slight_smile: