简体   繁体   English

无法运行jar文件?

[英]Can't run jar file?

I'm new to java so I might be missing something obvious. 我是java的新手,所以我可能会遗漏一些明显的东西。 I'm trying to create a simple command-line game in java. 我正在尝试用java创建一个简单的命令行游戏。 I use two classes: 我使用两个类:

http://pastebin.com/wRgqgJQP http://pastebin.com/wRgqgJQP

http://pastebin.com/0rXbxdiJ 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: 当我尝试运行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? 任何想法如何解决这一问题? I'm thinking that the problem is related to using the sysout print thing...but Im not sure. 我在想这个问题与使用sysout打印的东西有关......但我不确定。 Thanks! 谢谢!

A NullPointerException indicates that a variable was null when being used with either a . NullPointerException指示当与a一起使用时变量为null . or an array reference like [0] . 或类似[0]的数组引用。

The stack trace tells us it happened "at game.GameHelper.getUserInput(GameHelper.java:12) ". 堆栈跟踪告诉我们它发生在“game.GameHelper.getUserInput(GameHelper.java:12)”。 Your source listing has this line at line 12 in GameHelper. 您的源列表在GameHelper的第12行有此行。

if (inputLine.length() == 0)

There is only one . 只有一个. telling us that inputLine was null . 告诉我们inputLinenull How did that happen? 那是怎么发生的? Well, it was assigned in line 11. 好吧,它是在第11行分配的。

inputLine = is.readLine();

So. 所以。 readLine() returned null. readLine()返回null。 How did that happen? 那是怎么发生的? Well, from http://download.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine () 那么,来自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 返回:包含行内容的String,不包括任何行终止字符;如果已到达流的末尾,则返回null

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. 该流是从System.in构建的,因此需要其他信息来说明可能的原因。

The way to debug ANY nullpointerexception 调试任何nullpointerexception的方法

1) Go to the line. 1)去线。 2) Look at each method call on that line - is is possible that a method called on an object where the object is null ? 2)查看该行上的每个方法调用 - 是否有可能在对象为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. 在您的特定情况下,行“if(inputLine.length()== 0)”抛出空指针异常。 Thus, you should make sure that "inputLine" is not null .... 因此,您应该确保“inputLine”不为null ....

Any particular reason why you aren't using the Scanner class? 您没有使用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();
    }
}

And if all you ever want it to do is use the result to ParseInt, you could change 如果你想要它做的就是将结果用于ParseInt,你可以改变

String inputLine = scan.nextLine()

to

int inputNumber = scan.nextInt()

and obviously change the return type from String to int 并且显然将返回类型从String更改为int

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 / catch中就是这样

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

and that should help 这应该有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM