Ser*_*pov 3 .net c# list 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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将新镜头放入播放器的镜头列表中时,我得到NullReferenceException:
Player pl = new Player("Nick",50);
pl.shots.Add(new Shot(pl.yPos)); // this line throws exception
Run Code Online (Sandbox Code Playgroud)
可能最终会变得简单.