I have a very simple rule that should loop over an array to retrieve values:
rule "ListTest"
when
Item TestButton changed
then
var String myTest = "v1 v2 v3 v4 v5 v6 v7 v8"
// create array from string
var list = myTest.split(" ")
// initialize counter var
var Number loopCounter = 0
// decleare single item in list
var String listItem
// break criteria in loop
var boolean flag = true
while(flag) {
logInfo("LoopTest", "loopCounter: " + loopCounter + " size: " + list.size())
listItem = list.get( loopCounter )
logInfo("LoopTest", "listItem: " + listItem)
loopCounter = loopCounter + 1
if (loopCounter >= 7){
flag = false
}
}
end
When I run this rule I see an error which I do not understand at all:
[INFO ] [org.eclipse.smarthome.model.script.LoopTest ] - loopCounter: 0 size: 8
[INFO ] [org.eclipse.smarthome.model.script.LoopTest ] - listItem: v1
[INFO ] [org.eclipse.smarthome.model.script.LoopTest ] - loopCounter: 1 size: 8
[ERROR] [.model.rule.runtime.internal.engine.RuleEngineImpl] - Rule 'ListTest': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ArrayExtensions.get(T[],int) on instance: null
From the logs I can see that I’m able to get the value for loopCounter = 0, but after the increment of loopCounter I get an error. When I address the elements with fixed values (e.g. list.get(5) ) it works.
I suspect that I’m doing some really sill mistake, but I have no idea what it could be.
Do you have any hint for me?
Cool, that was it.
What I still don’t understand is why it worked for loopCounter = 0. Datatypes in rules are still a mystery to me
Thank you so much!
Best,
Jens
A while loop is probably about the worst approach to doing something like this. I only mention it in case you are working up to using this while loop for a real world problem instead of just exploring the language.