1

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)

Raj
  • 29
  • 1
  • 2
  • 1
    Possibly helpful: https://stackoverflow.com/questions/4203646/system-console-returns-null – Roope Dec 02 '18 at 00:57
  • This is an eclipse problem, not related to groovy. https://stackoverflow.com/questions/4203646/system-console-returns-null covers a bit. Since you are trying to learn groovy , you may use groovy console - that is part your groovy installation : http://groovy-lang.org/groovyconsole.html – Jayan Dec 02 '18 at 03:31
  • Thanks Jayan, I am already using Groovy console and I have put the error in groovy console in my question – Raj Dec 02 '18 at 12:03

1 Answers1

1

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()

j.ferlic
  • 11
  • 1