从null / Map生成器生成null的游戏错误中更新循环

时间:2014-08-14 01:19:22

标签: java maps generator updates text-based

感谢您抽出宝贵时间阅读我的问题。我目前正在开发一款基于文本的小型冒险游戏。现在我在更新地图时收到此错误消息:

    Exception in thread "main" java.lang.NullPointerException
at main.GameManager.update(GameManager.java:92)
at main.GameManager.run(GameManager.java:78)
at main.Main.main(Main.java:8)

我有一个关于为什么会发生这种情况的理论,因为我花了几个小时试图自己解决这个问题。我的理论是,地图生成器正在创建一个具有空值的正方形,这会导致更新方法遇到错误。我注意到的另一件事是,每次都会出现随机对象的错误消息。有时,当武器返回错误消息时,实体返回错误消息。更新方法的代码如下:

注意:我在错误消息返回错误

的代码行上放了两个星号
    //method executed every loop until end
private void update(Object[][] gameMap, GUIController guiController){
    player.update(gameMap, guiController);

    //updates every object on the map besides player
    for(int row = 0; row<mapManager.getWIDTH(); row++){
        for(int col = 0; col<mapManager.getHEIGHT(); col++){

            //if the square has something inside of it
            if(!(gameMap[row][col] instanceof EmptySquare)){
                **gameMap[row][col].update(gameMap, guiController);
            }

        }
    }

    //check
    guiController.displayMessage("updated");
}

地图生成代码如下:

    for(int row = 0; row < WIDTH; row++){
        for(int col = 0; col < HEIGHT; col++){
            Random r = new Random();
            //NOTE: +1 included to get number 1-6 instead of 0-6 or 0-5
            int rand = r.nextInt(6) + 1;
            int rand2 = r.nextInt(2) + 1;
            System.out.println(rand);
            System.out.println(rand2);
            if(rand <= 2){
                //an empty tile
                gameMap[row][col] = new EmptySquare("Empty Square", 0);
                gameMap[row][col].setLocation(row, col);
            }else if(rand == 3 || rand == 4){
                //monster tile
                //decides what monster to put in the tile base on level
                if(row <= 2 && col <= 2){
                    switch(rand2){
                        case 1: gameMap[row][col] = new Entity("Snake", 1, row, col, 5, Weapon.weaponList[0]);
                        break;
                        case 2: gameMap[row][col] = new Entity("Flesh Eating Caterpillar", 1, row, col, 7, Weapon.weaponList[0]);
                        break;
                    }
                }else if((row > 2 && col > 2) && (row <= 5 && col <=5)){
                    switch(rand2){
                        case 1: gameMap[row][col] = new Entity("Spider", 2, row, col, 12, Weapon.weaponList[1]);
                        break;
                        case 2: gameMap[row][col] = new Entity("Zombie", 2, row, col, 20, Weapon.weaponList[1]);
                        break;
                    }
                }else if((row > 5 && col > 5) && (row <= 8 && col <= 8)){
                    switch(rand2){
                        case 1: gameMap[row][col] = new Entity("Giant", 3, row, col, 30, Weapon.weaponList[2]);
                        break;
                        case 2: gameMap[row][col] = new Entity("Fire Drake", 3, row, col, 40, Weapon.weaponList[3]);
                        break;
                    }
                }
            }else if(rand >= 5){
                //weapon tile
                //decides which monster to put int he tile based on level
                if(row <= 2 && col <= 2){
                    switch(rand2){
                        case 1: gameMap[row][col] = new Weapon("Wood Sword", 1, row, col, Material.WOOD);
                        break;
                        case 2: gameMap[row][col] = new Weapon("Bronze Sword", 1, row, col, Material.BRONZE);
                        break;
                    }
                }else if((row > 2 && col > 2) && (row <= 5 && col <=5)){
                    switch(rand2){
                        case 1: gameMap[row][col] = new Weapon("Steel Sword", 2, row, col,  Material.STEEL);
                        break;
                        case 2: gameMap[row][col] = new Weapon("Refined Steel Sword", 2, row, col, Material.REFINEDSTEEL);
                        break;
                    }
                }else if((row > 5 && col > 5) && (row <= 8 && col <= 8)){
                    switch(rand2){
                        case 1: gameMap[row][col] = new Weapon("Gold Sword", 3, row, col, Material.GOLD);
                        break;
                        case 2: gameMap[row][col] = new Weapon("Emerald Sword", 3, row, col, Material.EMERALD);
                        break;
                    }
                }
            } 

        }
    }

任何帮助都表示赞赏,因为我一直在努力工作几个小时而没有任何优势。如果您需要更多代码,对代码有任何疑问,或者对此有任何疑问,请询问。我的首要目标是弄清楚这个错误,了解错误,并了解可以采取哪些措施来防止错误再次发生。

谢谢

2 个答案:

答案 0 :(得分:2)

我认为gameMap最初都是null个值。您正尝试根据2个随机值为游戏地图中的每个位置指定不同类型的方块。

然而,有某些行,col,rand,rand2的组合,其中没有赋值。

例如,如果row == 0,col == 5,rand == 3,rand2 == 1则

(rand == 3 || rand == 4) is true
(row <= 2 && col <= 2) is false
(row > 2 && col > 2) && (row <=5 && col <= 5) is false
(row > 5 && col > 5) && (row <=8 && col <= 8) is false

由于此组合没有条件指定一种方形,gameMap[0][5]将为空。

例如,将以下内容添加到内循环的末尾:

if (gameMap[row][col] == null) {
  // Debug statement showing which row, col pairs weren't assigned
  System.out.println("(" + row + ", " + col + ") was not assigned!");
  gameMap[row][col] = new EmptySquare("Empty Square", 0);
  gameMap[row][col].setLocation(row, col);
}

答案 1 :(得分:0)

ArrayIndexOutOfBoundsException 表示您正在尝试访问广场中不存在的坐标( col ) - 不是广场本身是空的。我建议在发生异常的行之前打印出 row col 的值。你应该看到一些可疑的东西(例如程序试图在4x4平方中访问(3,4),范围从(0,0)到(3,3))。如果您仍然无法查看错误,请发布 generateMap 方法并指出第45行。