'Groovy map, get, variable, Jenkins

I have a problem with mapping in Groovy. I would like to get a value based on a variable.

def function(){
    map = [
         'test1': '1234',
         'test2': '4567'
    ]
    var=test1
    def result = map.get.("$var")
    return result
}

But, unfortunately. I always get back:

Cannot get property '[test1]' on null object



Solution 1:[1]

You are making a HashMap behind the scenes here and the way you are accessing it map.get.["$var"] Groovy is trying to access a key called "get" on your variable map.

You just want map["$var"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Morgan Eason