[SOLVED] Could not invoke equals on null


I have problem with one comparison that tries to compare against null and I can not find out how the null get there. Now I’m out of ideas where the problem is. The relevant part of code is this one:

rule "South side re-calculation"
logInfo("DEBUG", "TEST2.3.2:"+"local_RAIN="+local_RAIN.state+" local_rain_protect="+local_rain_protect.state+" local_RAIN="+local_RAIN.state+" local_SLD="+local_SLD.state+" local_clean_chn="+local_clean_chn+" HOME_AWAY="+HOME_AWAY.state+" DAY_NIGHT="+DAY_NIGHT.state+" local_calculated_run_direction="+local_calculated_run_direction+" local_stupid_variable"+local_stupid_variable+".")
if ((((local_RAIN.state == 1) && (local_rain_protect.state == 0)) || (local_RAIN.state == 0)) && (local_SLD.state == 0) && ((local_clean_chn == 2) || (local_clean_chn == 0)) && (HOME_AWAY.state == OFF) && (DAY_NIGHT.state == ON) ) { local_calculated_run_direction = 2 }
logInfo("DEBUG", "TEST2.5")

Log output says:

2020-02-14 00:04:17.650 [INFO ] [eclipse.smarthome.model.script.DEBUG] - TEST2.3.2:local_RAIN=0 local_rain_protect=1 local_RAIN=0 local_SLD=0 local_clean_chn=0 HOME_AWAY=OFF DAY_NIGHT=ON local_calculated_run_direction=0 local_stupid_variablenull.
2020-02-14 00:04:17.655 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'South side re-calculation': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_equals(int,int) on instance: null

In the log debug are all variables used in comparison listed, none of them is null, to make sure I added local_stupid_variable with null value so I can be sure how null is displayed in log and it is displayed as null.
Can someone see where the troublemaking null is?
If it helps I’m uploading also complete items, rules etc… but they are quite big.

Using openhab 2.5.1. on windows and linux (raspbian), both are showing the same issue.rollershutter.rules.txt (24.2 KB)
default.items.txt (4.5 KB)

Comparisons in rules are bit funny. As I understand it, you might view it that there are a set of “==” functions - one for strings, one for integers, one for ON/OFF states etc. The rule parser picks which one to try based on the first operand, and then attempts to convert the other to match. If that conversion is not possible, you get a null

Short version; comparison complaints about null may really be about operand type mismatch.
Try casting your numeric states to Numbers e.g.
if ((((local_RAIN.state as Number == 1) && ... etc

Thank you for the hint, but the error is still the same. I tried to put all the numbers as Number and now the if looks like

if ((((local_RAIN.state as Number == 1) && (local_rain_protect.state as Number == 0)) || (local_RAIN.state as Number == 0)) && (local_SLD.state as Number == 0) && ((local_clean_chn as Number == 2) || (local_clean_chn as Number == 0)) && (HOME_AWAY.state == OFF) && (DAY_NIGHT.state == ON) ) { local_calculated_run_direction = 2 }

Result is still the same:

2020-02-14 00:47:22.209 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'South side re-calculation': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_equals(int,int) on instance: null

But the above is without casting switches, I tried to use asSwitch, but got error:

2020-02-14 00:52:47.904 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'South side re-calculation': Could not cast ON to void; line 178, column 249, length 25

You don’t need to do that, because e.g keyword OFF is a .state

For heaven’s sake, simplify your problem.
Perform a single comparison for diagnostic purposes.

1 Like

Yes, I know it is hard when there is so many elements in if, will create one by one compare. I tried yesterday to isolate the problem, but was too tired and it seems to me that the system was behaving not deterministic way. Will do this today evening.

Only if you are interested (as I already wrote I was to tired to do it properly): There are several comparisons, one on each line and yesterday when I try to isolate the problem by removing elements from comparison on line 140, it starts to fail on line 135 which I have not touched (numbers are only examples now can not look at the code). Which was too much for my brain.

What is local_clean_chn?

The whole rule might help…

It’s attached to the OP, but make sure you are sitting down when you read it. :grimacing:

I tried to isolate the problem, but without any success, any isolation attempt resulted in working code.
If I have changed only one if line the error has jumped to another line (even before the fixed one).
I suspect some strange (=unknown to me) behavior of interpreter was the cause as when I added as Number to ALL comparisons the error disappeared.
Thank you for your help!