I have this piece of code and it’s causing NullPointerException
private List<Player> playerList;
private List<String> playerName;
Bundle b = getIntent().getExtras();
this.currentGame = b.getParcelable("created_tournament");
playerList = currentGame.getPlayers();
for (int i = 0; i < playerList.size(); i ++) {
this.playerName.add(this.playerList.get(i).getName());
Log.d(TAG, "Input was not a number!" + this.playerList.get(i).getName());
}
with playerList is List<Player> taken from a Parcelable object and playerName is List<String>.
Theoretically, this.playerName.add(this.playerList.get(i).getName()); should works since Log.d(TAG, "Input was not a number!" + this.playerList.get(i).getName()); works fine without causing any exception
Any idea?
The
playerNamelist in not initialized, and you call the methodaddon it while it’snull.Change that:
to :