我有两个类:
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
可能是一些最终简单的东西。
最佳答案
在你的Player
构造函数,只需初始化 shots = new List<Shot>();
关于c# - NullReferenceException 和我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221464/