当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Update loop inside game error from null/ Map generator generating null

问题描述:

Thanks for thanks the time to read my question. I am currently having trouble with a small text based adventure game I am developing. Right now I am getting this error message when I update my map:

 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)

I have a theory as to why this may be happening, as I have spent a few hours trying to figure this out on my own. My theory is that the map generator is creating a square with a null value which is causing the update method to encounter an error. Another thing I have noticed is that the error message occurs with random objects every time. Sometimes, an entity returns the error message, when other times a weapon returns the error message. The code for the update method is below:

NOTE: I put two asterisks on the line of code where the error message returned the error

 //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");

}

The map generation code is below:

 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;

}

}

}

}

}

Any help is appreciated as I have been working on this for hours with no prevail. If you need more code, have any questions about the code, or any questions at all for that matter please ask. My number 1 goal is figuring out this mistake, learning about what was wrong, and learning about what can be done to prevent it from happening again.

Thanks

网友答案:

I assume that gameMap is initally all null values. You are trying to assign different types of squares to each location in game map based on 2 random values.

However there are certain combinations of row, col, rand, rand2, where no value is being assigned.

For example, if row == 0, col == 5, rand == 3 and rand2 == 1 then

(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

Because there is no condition for this combination that assigns a type of square, gameMap[0][5] will be null.

As an example, add the following to the end of you inner loop:

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);
}
网友答案:

ArrayIndexOutOfBoundsException means that you are trying to access a coordinate (row, col) which doesn't exist in the square - not that the square itself is null. I would suggest printing out the value of row and col right before the line where the exception takes place. You should see something suspicious (e.g. program trying to access (3,4) in a 4x4 square, the range is from (0,0) to (3,3)). If you're still unable to see what's wrong, post the generateMap method and point out line 45.

分享给朋友:
您可能感兴趣的文章:
随机阅读: