无法运行jar文件?

发布于 2024-12-12 09:34:15 字数 639 浏览 0 评论 0原文

我是java新手,所以我可能会遗漏一些明显的东西。我正在尝试用 java 创建一个简单的命令行游戏。我使用两个类:

http://pastebin.com/wRgqgJQP

http://pastebin.com/0rXbxdiJ

第一个处理用户输入,第二个运行数学问题游戏。 当我尝试运行 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:

http://pastebin.com/wRgqgJQP

http://pastebin.com/0rXbxdiJ

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山川志 2024-12-19 09:34:15

NullPointerException 表示变量在与 . 或数组引用(如 [0])一起使用时为 null。

堆栈跟踪告诉我们它发生在“game.GameHelper.getUserInput(GameHelper.java:12)”
"。您的源列表在 GameHelper 的第 12 行有这一行。

if (inputLine.length() == 0)

只有一个 . 告诉我们 inputLinenull。这是怎么做到的发生了吗?它是在第 11 行分配的。

inputLine = is.readLine();

那么,这是怎么发生的呢? 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()

返回:包含该行内容的字符串,不包括
任何行终止字符,如果流末尾有则为 null
已达到

因此已到达流的末尾。该流是从 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.

if (inputLine.length() == 0)

There is only one . telling us that inputLine was null. How did that happen? Well, it was assigned in line 11.

inputLine = is.readLine();

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

Returns: A String containing the contents of the line, not including
any line-termination characters, or null if the end of the stream has
been reached

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.

囍笑 2024-12-19 09:34:15

调试ANY nullpointerException的方法

1) 转到该行。
2) 查看该行上的每个方法调用 - 是否有可能在对象为 null 的情况下调用该方法?

a=null ;
a.setX("X");

will result in a null pointer exception.  

在您的具体情况下,“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 ?

a=null ;
a.setX("X");

will result in a null pointer exception.  

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 ....

暮年慕年 2024-12-19 09:34:15

您不使用 Scanner 类的任何具体原因是什么?

package game;
import java.util.Scanner;
public class GameHelper {
    public String getUserInput(String prompt)
    {
        System.out.print(prompt + "  ");
        Scanner scan = new Scanner(System.in);
        String inputline = scan.nextLine();
        return inputLine.toLowerCase();
    }
}

如果您希望它做的只是将结果用于 ParseInt,您可以更改

String inputLine = scan.nextLine()

int inputNumber = scan.nextInt()

,并且显然将返回类型从 String 更改为 int

Any particular reason why you aren't using the Scanner class?

package game;
import java.util.Scanner;
public class GameHelper {
    public String getUserInput(String prompt)
    {
        System.out.print(prompt + "  ");
        Scanner scan = new Scanner(System.in);
        String inputline = scan.nextLine();
        return inputLine.toLowerCase();
    }
}

And if all you ever want it to do is use the result to ParseInt, you could change

String inputLine = scan.nextLine()

to

int inputNumber = scan.nextInt()

and obviously change the return type from String to int

随风而去 2024-12-19 09:34:15

试试这个

package game;
import java.io.*;
public class GameHelper {

    public String getUserInput(String prompt) {
        String inputLine = null;
        System.out.print(prompt + "  ");
        try {
            BufferedReader is = new BufferedReader(new InputStreamReader(
                    System.in));
            inputLine = is.readLine();
            if (inputLine == null)
                return null;
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
        return inputLine.toLowerCase();
    }


}

来修复 try/catch 中的其他错误,

 numProbs=(Integer.parseInt(numProbsReader))

就像这样

try{
 numProbs=(Integer.parseInt(numProbsReader))
}catch(Exception e){System.err.println("Invalid Input");}

,这应该会有所帮助

Try this

package game;
import java.io.*;
public class GameHelper {

    public String getUserInput(String prompt) {
        String inputLine = null;
        System.out.print(prompt + "  ");
        try {
            BufferedReader is = new BufferedReader(new InputStreamReader(
                    System.in));
            inputLine = is.readLine();
            if (inputLine == null)
                return null;
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
        return inputLine.toLowerCase();
    }


}

To fix your other error surround

 numProbs=(Integer.parseInt(numProbsReader))

in try/catch so like this

try{
 numProbs=(Integer.parseInt(numProbsReader))
}catch(Exception e){System.err.println("Invalid Input");}

and that should help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文