0
當我嘗試創建一個機器人類,我得到以下錯誤的特定對象,我不明白是什麼問題:遇到問題創建一個對象
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!!");
}
}
}
爲機器人類的代碼目前是:
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'的構造函數中。 –
沒有看到bot類的代碼,沒有辦法告訴可能有什麼錯誤。 – jpw
你的代碼中是否有靜態初始化器初始值設定項? – lakshman