NullReferenceException和我不知道为什么

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)

可能最终会变得简单.

dia*_*aho 6

Player构造函数中,只需初始化shots = new List<Shot>();