我昨天开始学习 groovy,到目前为止我一直很喜欢它,直到我读到本书的一部分,我必须使用 console().readLine() 从用户那里获取输入,但我一直收到这个错误
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)
我已经在 intelliJ 中将其作为 groovy 脚本和类进行了尝试。我什至继续在 groovyConsole 中尝试它,但它仍然会引发错误。
这是代码
print("What is your name ");
def fName = System.console().readLine()
println("Hello" + fName)
谢谢
最佳答案
如果您使用任何 Groovy IDE,您将无法使用输入/输出控制台,因此会抛出空指针异常,因此您可以使用以下命令,如果您使用的是 Intellij,请从工具 -> groovy 控制台启用 groovy 控制台用于 I/O。
println "What is your name?"
println "Your name is:"+System.in.newReader().readLine()
关于intellij-idea - Console.ReadLine() 在 Groovy 中抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47348142/