Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a MissingPropertyException in the GroovyConsole?

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)
like image 643
Michael Avatar asked Dec 02 '22 14:12

Michael


1 Answers

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.

like image 142
Haider Ali Avatar answered Dec 18 '22 15:12

Haider Ali