[SOLVED] -Accessing stringbuilder in a loop

Tags: #<Tag:0x00007f616d373798> #<Tag:0x00007f616d3736a8>

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#|::0.2.0.2.0.8.1.0.7::0::/1)

==> /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: