2011-11-20 92 views
-2

我有這樣一段代碼,它是造成NullPointerException異常的Android清單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()); 
}   

與playerList從Parcelable對象採取List<Player>playerNameList<String>

從理論上講,應該this.playerName.add(this.playerList.get(i).getName());因爲Log.d(TAG, "Input was not a number!" + this.playerList.get(i).getName());作品能正常工作,而不會造成任何異常

任何想法?

+0

它總是一個好主意,指出該行尋求幫助時導致異常。 –

回答

4

playerName列表中未初始化,並且您調用它的方法add而它的null

變化是:

private List<String> playerName; 

到:

private List<String> playerName = new ArrayList<String>(); 
相關問題