我的 Java 游戏有问题。当我想导出它时,它不会加载世界。我会得到一个“线程 - 0”异常。这是我的功能:
private void loadWorld(String path){
String file = World.class.getResourceAsStream(path).toString();
file.toString();
String[] tokens = file.split("\\s+");
width = Utils.parseInt(tokens[0]);
height = Utils.parseInt(tokens[1]);
spawnX = Utils.parseInt(tokens[2]);
spawnY = Utils.parseInt(tokens[3]);
tiles = new int[width][height];
for(int y = 0;y < height;y++){
for(int x = 0;x < width;x++){
tiles[x][y] = Utils.parseInt(tokens[(x + y * width) + 4]);
}
}
}
它需要作为字符串加载,因为我需要将其中的每个字符串拆分为一个数组。管理这个的正确方法是什么?
编辑: 这是堆栈跟踪:
C:\Users\User>java -jar C:\Users\User\Desktop\Game.jar
Exception in thread "Thread-0" java.lang.NullPointerException
at dev.codenmore.tilegame.worlds.World.loadWorld(World.java:76)
at dev.codenmore.tilegame.worlds.World.<init>(World.java:36)
at dev.codenmore.tilegame.states.GameState.<init>(GameState.java:14)
at dev.codenmore.tilegame.Game.init(Game.java:61)
at dev.codenmore.tilegame.Game.run(Game.java:94)
at java.lang.Thread.run(Unknown Source)