无法创建对象

时间:2014-02-22 14:57:09

标签: java oop exception object runtime-error

当我尝试为bot类创建特定对象时,我收到以下错误,我不明白问题是什么:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at Bot.<init>(Bot.java:10)
at PlayGame.<clinit>(PlayGame.java:5)

我的主要课程代码如下:

import java.util.Scanner;

public class PlayGame {
static GameLogic GLObject = new GameLogic();
static Bot botObject = new Bot();
public static void main(String [] args){
    System.out.println("Enter the file name and extension in the form file_name.extension:");

    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();

    Map mapObject = new Map();
    mapObject.readMap(fileName);

    while(true){
        getMove();
    }
}

public static void getMove(){
    System.out.println("Enter what you wish to do:");
    Scanner scanner_1 = new Scanner(System.in);
    String move = scanner_1.nextLine();
    move = move.toUpperCase();
    useMove(move);


}

public static void useMove(String move){
    if(move.equals("N")){
        GLObject.MOVE('N');
    }
    else if(move.equals("E")){
        GLObject.MOVE('E');
    }
    else if(move.equals("S")){
        GLObject.MOVE('S');
    }
    else if(move.equals("W")){
        GLObject.MOVE('W');
    }
    else if(move.equals("HELLO")){
        GLObject.HELLO();
    }
    else if(move.equals("PICKUP")){
        GLObject.PICKUP();
    }
    else if(move.equals("LOOK")){
        GLObject.LOOK();
    }
    else if(move.equals("QUIT")){
        GLObject.QUIT();
    }
    else if(move.equals("BOT")){
        botObject.getMap();
    }
    else{
        System.out.println("This is not a valid input!!");
    }
}

}

目前,bot类的代码是:

public class Bot {
GameLogic GLObject = new GameLogic();
char [][] Array;
int Column = GLObject.Column;
int Row = GLObject.Row;
boolean goldMarker = false;
int goldNumber = Integer.parseInt(Map.goldNumber);
int goldCount = 0;
boolean exitSet = false;

public void getMap(){
    Array = GameLogic.mapArrayGlobal;
    GameLogic.printArray(Array);
    traverseMap();
}

public void traverseMap(){
    int direction = (int) (Math.random() * 4);
    if(direction == 0){
        MOVE('N');
    }
    else if(direction == 1){
        MOVE('S');
    }
    else if(direction == 2){
        MOVE('E');
    }
    else if(direction == 3){
        MOVE('W');
    }
}

有人可以告知造成这个问题的原因。

非常感谢:)

1 个答案:

答案 0 :(得分:0)

您的问题是Map.goldNumber在构造Bot时不是整数,导致尝试使用parseInt时初始化失败。如果确保Map.goldNumber中有一个有效的数字字符串,它应该有用。