Cannot assign a value in null context. and HashMap - what is wrong?

I have following code:

Variable definition:

var Map<String,Timer> WtrTimers = newHashMap( 
	"WtrValveFront_StartTimer" -> null,
	"WtrValveFront_StopTimer"  -> null,
	"WtrValveBack_StartTimer" -> null,
	"WtrValveBack_StopTimer"  -> null,
	"WtrValveLine_StartTimer" -> null,
	"WtrValveLine_StopTimer"  -> null,	
	"Wtr_StopTimer"			  -> null
 )

In rule:

 if(WtrTimers == null) {
		logError("FILE", "Wtr_starting_stoping: WtrTimer was not initialized")
		WtrTimers = newHashMap( 
		"WtrValveFront_StartTimer" -> null,
		"WtrValveFront_StopTimer"  -> null,
		"WtrValveBack_StartTimer" -> null,
		"WtrValveBack_StopTimer"  -> null,
		"WtrValveLine_StartTimer" -> null,
		"WtrValveLine_StopTimer"  -> null,
		"Wtr_StopTimer"			  -> null
		)
		logInfo( "FILE", "Wtr_starting_stoping: After reinitializing WtrTimers[" + WtrTimers + "]")
	}

and in the log I’ve got:

2018-06-19 05:00:00.711 [ERROR] [.eclipse.smarthome.model.script.FILE] - Wtr_starting_stoping: WtrTimer was not initialized
2018-06-19 05:00:00.728 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Wtr_starting_stoping’: An error occurred during the script execution: Cannot assign a value in null context.

As you see WtrTimers == null returns null but “WtrTimers = newHashMap(…” raise error but why? It looks like normal initialization.

Michal Szymanski

Why bother initializing the Map in the first place? If the key you are searching for isn’t there it returns null anyway.

Just create the hashmap and skip all this initialization stuff.

Along those lines, I don’t think it will let you initialize the map with null because I think it actually removes that key value pair from the map when you set the value to null anyway.

1 Like