[SOLVED] -Accessing stringbuilder in a loop

I have a seemingly simple piece of JSON that I am attempting to parse (from Facebox running on Docker)

The following code is close to what I wish to achieve, but I am getting errors with the string builder:
From what I can make out, it appears that the stringBuilder object that I create ( val StringBuilder sb2 = new StringBuilder )is not accesible from within the for loop?( for (i : 0 .. (maxCount -1)){)

All that I am trying to do (for now) is extract matched face names from the json output.

Here is my current code:

   val StringBuilder sb2 = new StringBuilder
 val faceCount = transform("JSONPATH", "$.facesCount", json.toString)
 val maxCount = Integer::parseInt(faceCount).intValue
 val Number matches = 0
 logInfo("===JSON PARSER===", faceCount.toString +  " faces discovered")
 for (i : 0 .. (maxCount -1)){
   val faceConfidence = Float::parseFloat( transform("JSONPATH", "$.faces[" +i+"].confidence", json.toString)) as Number   
   if(faceConfidence > 0.5){
     logInfo("===JSON PARSER===", "Matching face " + i + " discovered")  
     matches = matches + 1
     val whosThere = (transform("JSONPATH", "$.faces[" +i+"].name", json.toString)).toString
     sb2.append(", " + whosThere )
     } else {
     logInfo("===JSON PARSER===", "Strange face " + i + " discovered")  

This is my json object

“success”: true,
“facesCount”: 3,
“faces”: [{
“rect”: {
“top”: 345,
“left”: 902,
“width”: 186,
“height”: 186
“matched”: false,
“confidence”: 0
}, {
“rect”: {
“top”: 231,
“left”: 230,
“width”: 268,
“height”: 267
“matched”: false,
“confidence”: 0
}, {
“rect”: {
“top”: 469,
“left”: 1109,
“width”: 185,
“height”: 186
“id”: “ouma2.JPG”,
“name”: “ouma”,
“matched”: true,
“confidence”: 0.5692880948411863

and lastly, this is the error I get in my logs:

09-12 16:04:09.450 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test Rule': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: _test.rules#|::

==> /log/events.log <==

Add some more logInfo statements to identify the problem statement. Another idea is to post the whole rule

Please How to use code fences instead of quotes for code.

I don’t think it has anything to do with the StringBuilder. It’s complaining about an = operation (assignValueTo). One of these assignments is has a void on the right hand side which cannot be assigned to a variable.

As Thomas suggests, add logging to narrow the error down to a specific line of code. Then we can go from there.

Might not be the cause, but you decare

val Number matches = 0

and then try to change it. Use var if you need to change the value of a variable.

Good catch. That could be it.

Thanks everyone for the feedback and attempts at helping.

Whilst trying to simplify the entire thing to a 6 line rule to demonstrate the issue, I actually managed to work out what the bug was.

The issue was that each time I tried to append anything to the stringbuilder item, even a string value, hardcoded, it would fail. I assumed that this was because the stringbuilder was in the loop, since the error appeared as soon as the sb was getting populated.
It turns out that it was actually failing because once the stringbuilder did contain something, it was allowing entry to ANOTHER piece of code.
This code of course, was rubbish (it simply used an undeclared variable)

Irrespective, I learnt a few new trhings in the bug hunt :slight_smile:

Hi @bulletprooffool,

I’m also interested in using Facebox for facial recognition.
It sounds like you were successful in getting everything running.
Care to share your set-up and solution?


So sorry - I had it going OK, but binned it :confused:
When I can look, I’ll see if I still have my docker files and rules somewhere.
My home automation box was struggling and I binned everything besides critical tools and then rebuilt. Found the face thing a little less perfect than I had hoped

Thanks @bulletprooffool!
What binding did you use to interface OH with facebox?