无法运行jar文件?
我是java新手,所以我可能会遗漏一些明显的东西。我正在尝试用 java 创建一个简单的命令行游戏。我使用两个类:
第一个处理用户输入,第二个运行数学问题游戏。 当我尝试运行 jar 文件(eclipse 文件运行正常)时,出现错误 - 无法启动,并打印出以下控制台:
Exception in thread "main" java.lang.NullPointerException
at game.GameHelper.getUserInput(GameHelper.java:12)
at game.MultGame.createGame(MultGame.java:18)
at game.MultGame.main(MultGame.java:12)
Any ideas how to fix this?我认为问题与使用 sysout print 相关......但我不确定。谢谢!
I'm new to java so I might be missing something obvious. I'm trying to create a simple command-line game in java. I use two classes:
The first handles user inputs, the second runs a math question game.
When I try to run the jar file (the eclipse file runs fine), I get an error - can't be launched, and the following console print out:
Exception in thread "main" java.lang.NullPointerException
at game.GameHelper.getUserInput(GameHelper.java:12)
at game.MultGame.createGame(MultGame.java:18)
at game.MultGame.main(MultGame.java:12)
Any ideas how to fix this? I'm thinking that the problem is related to using the sysout print thing...but Im not sure. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NullPointerException
表示变量在与.
或数组引用(如[0]
)一起使用时为 null。堆栈跟踪告诉我们它发生在“game.GameHelper.getUserInput(GameHelper.java:12)”
"。您的源列表在 GameHelper 的第 12 行有这一行。
只有一个
.
告诉我们inputLine
为null
。这是怎么做到的发生了吗?它是在第 11 行分配的。那么,这是怎么发生的呢? href="http://download.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine" rel="nofollow">http://download.oracle.com/javase /1.4.2/docs/api/java/io/BufferedReader.html#readLine()
因此已到达流的末尾。该流是从 System.in 构建的,因此需要额外的信息来说明原因。
A
NullPointerException
indicates that a variable was null when being used with either a.
or an array reference like[0]
.The stack trace tells us it happened "at game.GameHelper.getUserInput(GameHelper.java:12)
". Your source listing has this line at line 12 in GameHelper.
There is only one
.
telling us thatinputLine
wasnull
. How did that happen? Well, it was assigned in line 11.So.
readLine()
returned null. How did that happen? Well, from http://download.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine()So the end of the stream has been reached. The stream was constructed from System.in, so additional information is needed to tell why that may be.
调试ANY nullpointerException的方法
1) 转到该行。
2) 查看该行上的每个方法调用 - 是否有可能在对象为 null 的情况下调用该方法?
在您的具体情况下,“if (inputLine.length() == 0)”行引发空指针异常。因此,您应该确保“inputLine”不为空......
The way to debug ANY nullpointerexception
1) Go to the line.
2) Look at each method call on that line - is is possible that a method called on an object where the object is null ?
In your specific case, the line "if (inputLine.length() == 0)" is throwing a null pointer exception. Thus, you should make sure that "inputLine" is not null ....
您不使用 Scanner 类的任何具体原因是什么?
如果您希望它做的只是将结果用于 ParseInt,您可以更改
为
,并且显然将返回类型从 String 更改为 int
Any particular reason why you aren't using the Scanner class?
And if all you ever want it to do is use the result to ParseInt, you could change
to
and obviously change the return type from String to int
试试这个
来修复 try/catch 中的其他错误,
就像这样
,这应该会有所帮助
Try this
To fix your other error surround
in try/catch so like this
and that should help