Raj
Raj

Reputation: 29

Error while trying to get input in groovy

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)

Error in Groovy console

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)

Error in Eclipse

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

Answers (1)

j.ferlic
j.ferlic

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

Related Questions