Cannot assign a value in null context - strange behavior

OS: Raspberry
Openhab: 2.3

I have following code:

val Map<String,Timer> WtrTimers = newHashMap
.....
if(WtrTimers === null) {
     logError("FILE", "Wtr_starting_stoping: WtrTimer is not initialized")
     WtrTimers = newHashMap
      logInfo("FILE", "Wtr_starting_stoping: WtrTimer construction finished ")
    ....................................................

Sometimes after reboot or refreshing rule I’ve got such errror message

 Wtr_starting_stoping: WtrTimer is not initialized
2018-08-25 10:19:44.357 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Wtr_starting_stoping': An error occurred during the script execution: Cannot assign a value in null context.

It is litle strange because I’m trying initialize variable.

Michal Szymański

This might be related to the startup timing issue some have seen. What happened to us sometimes rules start evening before Items have been fully loaded it even the rules themselves have been fully loaded so you start to see errors like this or complaints about Items it global vars that do exist not existing.

You only see these during startup.

The only error I see is because you defined WtrTimers as a val, you cannot reassign it with a newHashMap. And there should never be a need to. If you need to reset the map just call WtrTimers.empty.

The best way I’ve seen to deal with this is to just let the Rule fail during startup. The next time the Rule runs it should work since everything will be loaded.

For System started Rules, use a try/catch and in the catch create a timer to retrigger the rule in a few seconds.

But my example I do not use any item.

You only see these during startup.

Sometime WtrTimers was not initialized during startup and even after few hours in the rule was still null and that is why I tried to once again initialize using newHashMap. Can I use WtrTimers.empty when WtrTimers object is null ? I want to initialize hashmap because is null not empty.

Michal Szymanski

so you start to see errors like this or complaints about Items or global vars that do exist not existing.

The only error I see is because you defined WtrTimers as a val, you cannot reassign it with a newHashMap

Change to use a var instead of a val.

Change to use a var instead of a val.

To be honest before it was var and I had the same problem , but I will check it once again.

Michal Szymanski

I’ve changed to:
var Map<String,Timer> WtrTimers = newHashMap // global variable

and in one rule:

		logInfo( "FILE", "Wtr_starting_stoping: WtrTimers[" + WtrTimers + "]")
		if(WtrTimers === null) {
			logError("FILE", "Wtr_starting_stoping: WtrTimer is not initialized ")
			WtrTimers =   newHashMap
			logInfo("FILE", "Wtr_starting_stoping: WtrTimer construction finished ")

but still in the log I have:

2018-09-28 05:00:07.543 [INFO ] [.eclipse.smarthome.model.script.FILE] - Wtr_starting_stoping: WtrTimers[null]
2018-09-28 05:00:07.551 [ERROR] [.eclipse.smarthome.model.script.FILE] - Wtr_starting_stoping: WtrTimer is not initialized
2018-09-28 05:00:07.558 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Wtr_starting_stoping': An error occurred during the script execution: Cannot assign a value in null context.

operation is very simple and I do not uderstand what is wrong and how to fix it or implement workaround :frowning:

Michał

Why are you recreating the Map in the first place? Typically one would create it one time and if for some reason you need to clear it out use WtrTimers.clear.

I suspect that the Rule is running into the known bug where Rules can trigger even before everything in that .rules file has finished loading. As a result globals may not yet be populated or even exist yet.

There are all sorts of work arounds posted on the forum (e.g. moving the .rules files back to the rules folder only after a certain amount of time has passed after initially starting). Until this startup timing bug gets fixed, we cannot expect Rules that trigger while OH is first starting to have a consistent, correct, or complete working environment and therefore strange errors like this will occur.

Because WtrTimers is null

2018-09-28 05:00:07.543 [INFO ] [.eclipse.smarthome.model.script.FILE] - Wtr_starting_stoping: WtrTimers[null]

and I cannot call clear on null object (does it work different in OH?).

It happens around 12h after starting OH :slight_smile:

You define and assign WtrTimers on one line as a global. It should never be null inside any Rule unless you are setting it to null somewhere.

Good to know but sometimes during initialization it display error message and after I I could not initialize once again :frowning:

I suspect what was happening is during startup it was null because of the aforementioned timing bug. But on subsequent runs of the Rule the Map should be there and not null.

If it is becoming null at a later time then either you have a Rule that is setting it to null or there is some bug in the Rules DSL that needs to be addressed.

1 Like

I will make test with this:

var Map<String,Timer> WtrTimers
createTimer(now.plusSeconds(120)) [|
WtrTimers = newHashMap

I’m not sure what that would be testing.

I thought that delay of initialization solve the problem. I think I rewrite the code and remove hashmap and use several variables instead.