SystemNullReferenceException:未设置对象引用

本文关键字:对象引用 设置 SystemNullReferenceException | 更新日期: 2023-09-27 17:51:12

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Quiz1
{
    public partial class Actor
    {
        public int x;
        public int y;
        public int box;
        public bool flag = false;
    }
public partial class Box
{
    public int countactors = 0;
    public int x;
    public int y;
}
public partial class Form1 : Form
{
    Bitmap pic;
    Box[] b;
    Actor[] a;
    Graphics g;
    int count = 0;
    public Form1()
    {
        b = new Box[3];
        a = new Actor[6];
        pic = new Bitmap("mine7es.png");
        this.WindowState = FormWindowState.Maximized;
        g = this.CreateGraphics();
        b[0].x = 0;
        b[0].y = 0;
        b[1].x = 310;
        b[1].y = 0;
        b[2].x = 620;
        b[2].y = 0;
        this.MouseDown += Form1_MouseDown;
        this.KeyDown += Form1_KeyDown;
        this.MouseMove += Form1_MouseMove;
    }
    void Form1_MouseMove(object sender, MouseEventArgs e)
    {
    }
    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    }
    void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left && count<6)
        {
            for (int i = 0; i < 3; i++)
            {
                if (e.X > b[i].x
                    && e.X < (b[i].x + 300)
                    && e.Y > b[i].y
                    && e.Y < (b[i].y + 300)
                    && b[i].countactors < 2)
                {
                    b[i].countactors++;
                    a[count].x = e.X;
                    a[count].y = (e.Y + 250);
                    a[count].flag = true;
                    count++;
                    drawscene();
                    break;
                }
            }
        }
    }
    void drawscene()
    {
        g.Clear(Color.White);
        for (int i = 0; i < 3; i++)
        {
            g.DrawRectangle(Pens.Black, b[i].x, b[i].y, 300, 300);
        }
        for (int i = 0; i < 6; i++)
        {
            if (a[i].flag == true)
            {
                g.DrawImage(pic, a[i].x, a[i].y);
            }
        }
        for (int i = 0; i < count; i++)
        {
            g.DrawImage(pic, a[i].x, a[i].y);
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
}

}

当我尝试运行时,我得到以下错误:

An unhandled exception of type 'System.NullReferenceException' occurred in Quiz1
Additional information: Object reference not set to an instance of an object`

指向b[0] = 0;

SystemNullReferenceException:未设置对象引用

当你创建"Box"数组时,你只是在创建一个引用数组。然后需要创建实际的对象:

b[0] = new Box();
b[0].x = 0;

您有Box数组,但实际上没有创建Box对象。在访问Box类的属性之前,必须先创建它的对象。

 b[0] = new Box();
 b[0].x = 0;

当试图解引用a时引发的异常

即使您将b设置为在b = new Box[3]行中具有值,您也不会将实际值分配给b[0],因此当尝试访问b[0].x以分配给它时,您在数组中的该位置引用空值。在抛出错误的行之前,插入类似以下内容的行:

b[0] = new Box();

或者使用任何适合Box类的构造函数

您还可以自动地用开始的项填充整个数组,以便下面引用b[1]b[2]的行不会抛出相同的错误:

for (int i =0;i < b.Length;i++)
{
    b[0] = new Box(); //again, use whatever constructor is correct for Box
}

这里所做的是初始化一个数组,但是用空值填充它。如果没有在数组中实例化类Box的类型,则不能访问其内部的任何属性。在访问x和y之前,您必须尝试以下操作:

b[0] = new Box();
b[1] = new Box();
b[2] = new Box();