为什么在Java中会出现nullPointerException?

为什么在Java中会出现nullPointerException?,java,Java,除非我单击StartName按钮,否则不会出现异常。我在helloworld.helloworld$3.handle(helloworld.java:123)的线程“JavaFX应用程序线程”java.lang.NullPointerException中得到了一个错误,上面写着Exception,我不知道这个错误是什么意思。错误发生在玩家行。添加(新玩家(0,“Amrit”);我做错了什么 public String gameStatus = "Pending"; public ArrayLis

除非我单击StartName按钮,否则不会出现异常。我在helloworld.helloworld$3.handle(helloworld.java:123)的线程“JavaFX应用程序线程”java.lang.NullPointerException中得到了一个错误,上面写着
Exception,我不知道这个错误是什么意思。错误发生在
玩家行。添加(新玩家(0,“Amrit”)
;我做错了什么

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";
                }

            }
    });

}
公共字符串gameStatus=“待定”;
公开的ArrayList玩家;
startGame.addEventHandler(MouseEvent.MOUSE_单击,
新的EventHandler(){
@重写公共无效句柄(MouseEvent e){
gameStatus=“已开始”;
玩家。添加(新玩家(0,“Amrit”);
添加(新玩家(1,“泰勒”);
添加(新玩家(2,“斯科特”);
添加(新玩家(3,“莱克”);
而(游戏状态=“已启动”){
//选择前两个定居点,每个定居点延伸一条道路
对于(int s=0;s<1;s++){
对于(int p=0;p
下面是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;

    }
}
包helloworld;
导入java.awt.Point;
导入java.util.ArrayList;
公开课选手{
公共int ID;
公共字符串名称;
public ArrayList hand=new ArrayList();
公共积分;
公共点[]城市建成;
公共点[]已建定居点;
公共国际城市协会;
公共内部结算有效期;
公共点[]道路建成;
公共道路安全;
播放器(int-id,字符串播放器名称){
ID=ID;
name=playerName;
}
}

玩家
未初始化,当您尝试使用它时,它为空。在中更改该段代码:

public ArrayList<Player> players = new ArrayList<>();
public ArrayList players=new ArrayList();

玩家
为空。不能将元素添加到不存在的列表中。