SystemNullReferenceException:未设置对象引用

时间:2014-03-21 16:24:16

标签: c#

这是我的代码:

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;行。

4 个答案:

答案 0 :(得分:2)

创建“Box”数组时,您只需创建一个引用数组。然后,您需要创建实际对象:

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

答案 1 :(得分:1)

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

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

尝试取消引用时引发的异常   null对象引用,MSDN

答案 2 :(得分:1)

即使您要将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
}

答案 3 :(得分:0)

你在这里做的是初始化一个数组,但用空值填充它。如果没有在数组中实例化类Box,则无法访问其中的任何属性。 在访问x和y之前,您必须尝试:

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