Reputation: 33
I started learning groovy yesterday and have been enjoying it thus far until I got to the part of the book where I have to take in inputs from the user using console().readLine() but I keep getting this error
Caught: java.lang.NullPointerException: Cannot invoke method readLine() on null object java.lang.NullPointerException: Cannot invoke method readLine() on null object
at com.usl.NewCodes.run(NewCodes.groovy:30)
I have tried it in intelliJ both as a groovy script and in a class. I even went on to try it in the groovyConsole yet it still throws an error.
Here is the code
print("What is your name ");
def fName = System.console().readLine()
println("Hello" + fName)
Thanks
Upvotes: 3
Views: 2835
Reputation: 2612
If you are using any IDE for Groovy, the input / output console will not be available to you hence throwing null pointer exception so, you can use the following, If you are using Intellij enable groovy console from Tools -> groovy console for I/O.
println "What is your name?"
println "Your name is:"+System.in.newReader().readLine()
Upvotes: 2