I have a custom class that includes some array lists, I initialize them in the constructor and then I have a method that adds to these arraylists, but when I run the code, I get this error: java.lang.NullPointerException
public class GameMenu {
private ArrayList<String> player1Moves;
private ArrayList<String> player2Moves;
private ArrayList<String> allMoves;
}
public GameMenu(long limit, Player player1, Player player2) {
this.player1Moves = new ArrayList<>();
this.player2Moves = new ArrayList<>();
this.allMoves = new ArrayList<>();
System.out.println("test");
}
private void writeDownMoves( Pieces piece , int xCordination , int yCordination ){
if ( isPlayer1sTurn == true ){
this.player1Moves.add("**" + piece + piece.getXCordination() + "," + piece.getYCordination() + " to " + xCordination + "," + yCordination + "**");
//the line above is where the program stops.
}
else{
this.player2Moves.add("**" + piece + piece.getXCordination() + "," + piece.getYCordination() + " to " + xCordination + "," + yCordination + "**");
}
this.allMoves.add("**" + piece + piece.getXCordination() + "," + piece.getYCordination() + " to " + xCordination + "," + yCordination + "**");
piece.setXCrdination(xCordination);
piece.setYCordination(yCordination);
}
I made sure that I had called the constructor by putting a System.out.println("test"); at the end of that constructor, so while these arraylists are initialized I still get this error. any. ideas about what the problem is?