I am completely new to Groovy and having some trouble just getting this program running. All I'm trying to do is add all the elements of one list into another, but I can't even access the variable I want in the method.
I looked around here for an answer, but all seemed to be just too specific to a person's problem or too technical for my limited understanding of Groovy. Any help is appreciated.
Using Groovy 2.4.5
in IntelliJ IDEA if that makes a difference. Here is the code I am trying to run:
class ListPractice implements Testable {
def mylist = [4,5,6]
/**
* Adds a set of elements to the mylist variable
*
* @paraelts The elements to be added
*/
def addToList(List elts) {
myList.each{
println "I: $it"
}
}
@Override
void testMe() {
addToList([7,8,9])
}
}
But it throws the following error:
Caught: groovy.lang.MissingPropertyException: No such property: myList
for class: ListPractice
Possible solutions: mylist
groovy.lang.MissingPropertyException: No such property: myList for class: ListPractice
Possible solutions: mylist
at ListPractice.addToList(ListPractice.groovy:14)
at ListPractice$addToList.callCurrent(Unknown Source)
at ListPractice.testMe(ListPractice.groovy:36)
at Testable$testMe.call(Unknown Source)
at RunMe$_main_closure1.doCall(RunMe.groovy:12)
at RunMe.main(RunMe.groovy:11)
Since this is my first time using the language and the structure of the code was done by my teacher, I'm not even sure if @Override
is necessary or what it does.
Capitalisation is important
You declared your list as mylist
Then you try to call each on myList
It shows you in the error message
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With