System started Rule shouts Null Error

Hello,

if I try to write a Rule as following:

rule "Initialize on System start"
when
System started
then
HashMap<String,String> myMap = new HashMap
end

the Log Message “Error during the execution of startup rule ‘Initialize on System start’ : null” comes after saving the rule. But why? Can’t I initialize a Map on System start?

Okay, at this point it was my fault. The initialization of the HashMap must be like:

HashMap<String,String> myMap = newHashMap

But now there is a new error message in the log:
“‘get’ is not a member of ‘java.util.HashMap’”

What does it mean? I want to use the command:

myMap.get("MyKey")

I’m surprised you are not selling syntax errors. You are missing the val. Also, defining it is a System started rule doesn’t do much for you as any variable defined in a Rule goes away as soon as the Rule exits.

You need to define it as a global if you want to use it across multiple Rules.

https://docs.openhab.org/configuration/rules-dsl.html#the-syntax

Finally, in my experience, there is usually a better way to achieve whatever you are after using Items and Groups and the Design Patterns.