Why does my List require braces when referring to an element?

On the 2nd to last line 'String sData = {DescList(index)} " will not work without the braces. It sends an error:


or

error groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.call() is applicable for argument types: (java.lang.Integer) values: [1] Possible solutions: tail(), tail(), take(int), take(int), wait(), last() on line 43 (method handlerButton)

/* List Test App*/

import groovy.transform.Field

definition(
    name: "List Test App", namespace: "hubitat", author: "JohnRob",
    description: "test some code", category: "Convenience", iconUrl: "", iconX2Url: "")

preferences {
    page(name: "mainPage")
}

def mainPage() {
    dynamicPage(name: "mainPage", title: " ", install: true, uninstall: true) {
        section {
            input "thisName", "text", title: "Name this test", submitOnChange: true
            if(thisName) app.updateLabel("$thisName")
            
			input "testButton", "capability.pushableButton",title: "Button Driver", submitOnChange: true, required: true, multiple: false
        } // section
    }   // dymanicPage
}   // mainPage

@Field static List<String> DescList = ['Outside Temp = ', 'Outside Humidity = ']

def installed() {
    initialize()
}

def updated() {
    unsubscribe()
    initialize()
}

def initialize() {
    subscribe(testButton, "pushed", handlerButton)
}

def handlerButton(evt) {
    int index = 1
    log.info "DescList index1:  ${DescList[index]}"
    String sData = {DescList(index)}				// why are the {braces} needed aroung DescList(index)
    log.info "sData:  ${DescList[index]}"
}
//  --- eof ---  

I see you are using parentheses around the word ‘index’ versus brackets [ ]. That’s likely your problem.

1 Like

No, I tried both. I've tried ( ) and receiving the same error.
When you suggested braces for the log.info variable portion I recalled reading they were recommended but usually not needed for simple structures. That's how I found the braces resolved my problem.

It's a little frustrating that in all my research (google, Groovy text, SmartThings docs etc) I could fine nothing that suggested they were necessary for my simple statement.

However thank you for you suggestions, And technically you solved my problem :grin:

1 Like

I would use this:

String sData = DescList[index]

// Or
String sData = DescList.get(index)

Also I usually use CAPS_SNAKE_CASE my static (constant) members (this is Java convention):

@Field static List<String> DESC_LIST = ['Outside Temp = ', 'Outside Humidity = ']
2 Likes

Thank you. That worked and I can clearly see the OOP concept.

I had tried a version of .get earlier however I'm guessing there was something else wrong as it didn't work.

However the below does not work. It results in an error (shown in the first screenshot in my original post) hence my frustration.

String sData = DescList[index]

1 Like