Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建对象时遇到问题_Java_Oop_Exception_Object_Runtime Error - Fatal编程技术网

Java 创建对象时遇到问题

Java 创建对象时遇到问题,java,oop,exception,object,runtime-error,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 B

当我尝试为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)
}

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');
    }
}
谁能告诉我是什么导致了这个问题


非常感谢:

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

查看堆栈跟踪。问题出在Bot的构造函数中。如果看不到Bot类的代码,就无法判断可能的错误。代码中是否有静态初始值设定项初始值设定项?Integer.parseIntMap.goldNumber;:这就是导致异常的原因,因为如消息所示,Map.goldNumber为空。旁白:在System.in上打开多个扫描仪可能不是最好的主意。扫描仪似乎在内部缓冲输入。
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');
    }
}