I get a null pointer exception when a call a method on a custom class. When i call my Map constructor on my driver i get a NPE error. I initialize my 2D array field in Map.java
public class Map {
private Field[][] gameMap;
private int row, col;
private int rowPlayer, colPlayer;
public Map(){
gameMap = new Field[10][20];
rowPlayer = 0;
colPlayer = 0;
gameMap[rowPlayer][colPlayer].setPlayerLoc(); //NPE happens here
}
}
Here is Field.java
public class Field {
private int playerLoc;
private char fieldType;
private int plantedDay, readyDay;
private char typeOfCrop;
public Field(){
fieldType = 'u';
playerLoc = 0;
plantedDay = 0;
}
public void setFieldUntilled(){
fieldType = 'u';
}
public void setFieldTilled(){
fieldType = 't';
}
public void setPlayerLoc(){
playerLoc = 1;
}
public void removePlayerLoc(){
playerLoc = 0;
}
public int getPlayerLoc(){
return playerLoc;
}
public void setPlantDay(int Day){
plantedDay = Day;
}
public void setReadyDay(int x){
readyDay = x;
}
}
I would like to know how to fix it and why the NPE happens. Thank you!