When I execute the following script in the GroovyConsole it gives me a MissingPropertyException
but I do not understand why:
def a = 'A'
def b() {
println a
}
b()
The following exception is thrown:
groovy.lang.MissingPropertyException: No such property:
a for class: ConsoleScript18
at ConsoleScript18.b(ConsoleScript18:3)
at ConsoleScript18.run(ConsoleScript18:5)
The reason behind that is when you write any thing outside function without declaring any class in groovy it is moved to main function.
So the scope of variable a
is limited to function main()
which you are trying to access in another function b()
of same class. But as there is no property a
for class it throws MissingPropertyException
.
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