-3

The exception doesn't come up until I click on the startGame button. I get the error saying Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at helloworld.HelloWorld$3.handle(HelloWorld.java:123) I have no idea what that error means. The error happens on the line players.add(new Player(0, "Amrit"));What am I doing wrong?

public String gameStatus = "Pending";
public ArrayList<Player> players;

    startGame.addEventHandler(MouseEvent.MOUSE_CLICKED, 
        new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent e) {

                gameStatus = "Started";
                players.add(new Player(0, "Amrit"));
                players.add(new Player(1, "Tyler"));
                players.add(new Player(2, "Scott"));
                players.add(new Player(3, "Ryker"));

                while(gameStatus == "Started"){

                    //Select first two settlements with one road extending from each
                    for(int s = 0; s < 1; s++){

                        for(int p = 0; p < players.size(); p++){

                            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                            alert.setTitle(players.get(p).name);
                            alert.setContentText(players.get(p).name + ", please select your first settlement and place a road next to it.");

                        }
                    }

                    gameStatus = "Ended";
                }

            }
    });

}

Here is Player.java:

package helloworld;

import java.awt.Point;
import java.util.ArrayList;

public class Player {

    public int ID;
    public String name;
    public ArrayList<String> hand = new ArrayList<String>();
    public int points;
    public Point[] citiesBuilt;
    public Point[] settlementsBuilt;
    public int citiesAvail;
    public int settlementsAvail;
    public Point[] roadsBuilt;
    public int roadsAvail;

    Player(int id, String playerName){

        ID = id;
        name = playerName;

    }
}
ShoeLace1291
  • 4,117
  • 11
  • 41
  • 65

1 Answers1

0

players is not initialised, when you try to use it is null. Change that piece of code in:

public ArrayList<Player> players = new ArrayList<>();
freedev
  • 17,230
  • 4
  • 83
  • 98