我收到 NullReferenceException 错误,但我不知道为什么?

I get NullReferenceException error but I don't know why?

我知道这个错误是在对象为空的地方显示的。但就我而言,我不确定为什么会显示它。 我尝试在 timer_tick 上每 30px 宽度随机创建 10 个 PictureBox 对象,这是我的代码。

PictureBox[] meteor;
int i=0;
Random rnd = new Random();

private void timer1_Tick(object sender, EventArgs e)
{
    if(i<10)
    { 
    int pozicija = rnd.Next(1, 25);
    pozicija *= 30;
    meteor[i] = new PictureBox()
    {
        Name = "pictureBox",
        BackColor = Color.Transparent,
        Size = new Size(80, 60),
        Location = new Point(pozicija, 0),
        Image = imageList2.Images[0],
    };
    this.Controls.Add(meteor[i]);
    }
    i++;
}

错误指向这行代码

this.Controls.Add(meteor[i]);

为什么 Visual studio 显示此错误?

使用前必须先实例化数组,如下所示:

PictureBox[] meteor = new PictureBox[10];

此外,我假设 imageList2 已定义并且您已将图像添加到其中。