I have asked a question about my code for a Game of Life Implementation. The suggested solution solved my problem but created a new one.
Now if I try to call the getCell() method I get a java.lang.NullPointerException.
How can I avoid this exception?
Link to my previous question with the corresponding code and solution code that I used:
How can I access the Cell Array in my getCell method? (Java)
Or if you just want the code:
public class GameMap {
private Cell[][] cellArray;
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}