[SOLVED] Variable behaviour I don’t understand

I have a voice control rule based on the example https://github.com/openhab/openhab/wiki/Controlling-openHAB-with-your-voice .
I doesnt work because it keeps on crashing on the newState variable.
Sniped of the (as I think) relevant code:

    var State newState = null
    if (command.contains("uit") || command.contains("stop")){
        logInfo("rules", "22 new state: "+newState)
        newState = OFF
        logInfo("rules", "24 new state: "+newState)

This code gives met the following output:

2016-03-01 21:00:46.968 [INFO ] [org.openhab.model.script.rules] - 22 new state: null
2016-03-01 21:00:46.980 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Voice control': Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null

I added the logInfo() around the newState update because my rule keeps crashing whenever I try to use newState after a state update. Why am I unable to use newState after an update?
Am I doing something wrong?

You can’t use it, because it is not instanced at first use in line 3.
The variable will first get a instance with the newState = OFF command in line 4!

You are completely right.
But my question is why I cant use it after line 4. After it gets an instance it throws me errormessages everywhere I use it. Including the logInfo() at line 5. Or do I understand you wrong?

Of course - a exception in a function/rule/method always interrupt these ones, when you don’t catch it with a try catch block. So the line 5 should not be reached.

@daveZ I think we don’t understand each other.

    var State newState = null
    if (command.contains("uit") || command.contains("stop")){
        logInfo("rules", "22 new state: "+newState)
        newState = OFF
        logInfo("rules", "Line 5 does get reached, its the next line where I try to use newState")
        logInfo("rules", "24 new state: "+newState)
2016-03-01 22:06:56.075 [INFO ] [org.openhab.model.script.rules] - 22 new state: null
2016-03-01 22:06:56.079 [INFO ] [org.openhab.model.script.rules] - Line 5 does get reached, its the next line where I try to use newState
2016-03-01 22:06:56.083 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Voice control': Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null

The code keeps on running till I try to use the variable newState. Proven by line 5.
Or am I not understanding what you mean?

Oh yeah, I see. My fault.
I tested it myself and it worked well with correct import of the used namespace import org.openhab.core.types.*

Did you import these namespace?

I got your described error if I do not import these namespace, otherwise it works well.

Oooops. :confused:
Your right, just missing that import. :sweat:

Thank you for your time! :grin: