-3
startGameボタンをクリックするまで例外が表示されません。私はエラーがException in thread "JavaFX Application Thread" java.lang.NullPointerException at helloworld.HelloWorld$3.handle(HelloWorld.java:123)
と言っています私はそのエラーが何を意味するか分かりません。エラーが発生しましたplayers.add(new Player(0, "Amrit"))
;私は間違っていますか?ここでJavaでnullPointerExceptionが発生するのはなぜですか?
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";
}
}
});
}
はPlayer.javaです:あなたはそれがnullで使用しようとすると
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;
}
}
'players'はnullです。存在しないリストに要素を追加することはできません。 – Manu