我有兩個類:
的NullReferenceException,我不知道爲什麼
class Player
{
public string Id { set; get; }
public int yPos { set; get; }
public List<Shot> shots;
public Player(string _Id, int _yPos)
{
Id = _Id;
yPos = _yPos;
}
}
class Shot
{
public int yPos { set; get; }
public Shot(int _yPos)
{
yPos = _yPos;
}
}
當我試圖把新的鏡頭在鏡頭的名單的球員,我得到的NullReferenceException:
Player pl = new Player("Nick",50);
pl.shots.Add(new Shot(pl.yPos)); // this line throws exception
可能最終很簡單。
pl.shots = new List(); pl.shots.Add(new Shot(pl.yPos)); –
AJP
2012-02-10 01:25:18