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