【问题标题】:Android List NullPointerExceptionAndroid 列表 NullPointerException
【发布时间】:2011-11-20 07:26:42
【问题描述】:

我有这段代码,它导致 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 是 List&lt;Player&gt; 取自 Parcelable 对象,playerNameList&lt;String&gt;

理论上,this.playerName.add(this.playerList.get(i).getName()); 应该可以工作,因为Log.d(TAG, "Input was not a number!" + this.playerList.get(i).getName()); 可以正常工作而不会导致任何异常

有什么想法吗?

【问题讨论】:

  • 在寻求帮助时指出哪一行导致异常总是一个好主意。

标签: android arraylist nullpointerexception


【解决方案1】:

playerName 列表未初始化,当它为null 时,您对其调用方法add

改变那个:

private List<String> playerName;

到:

private List<String> playerName = new ArrayList<String>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多