Reputation: 29
I am very new to groovy, I am using the below piece of code in Groovy console and Eclipse (running it as Groovy script), however getting the below error.
can you please help?
println("What is your name ");
def fName = System.console().readLine()
println("Hello" + fName)
groovy> print("What is your name ")
groovy> def fName = System.console().readLine()
groovy> println("Hello" + fName)
What is your name
Exception thrown
java.lang.NullPointerException: Cannot invoke method readLine() on null object
at ConsoleScript54.run(ConsoleScript54:2)
What is your name Caught: java.lang.NullPointerException: Cannot invoke method readLine() on null object java.lang.NullPointerException: Cannot invoke method readLine() on null object at sample.run(sample.groovy:2)
Upvotes: 1
Views: 792
Reputation: 11
System.console()
works when running in terminal, but shouldn't work in an IDE, like Eclipse. A better way of getting a single line of input would be System.in.newReader().readLine()
Upvotes: 1