简体   繁体   中英

Can't run jar file?

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!

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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