对象引用未设置为对象的实例(矩形数组)

时间:2014-02-01 22:11:21

标签: c# nullreferenceexception

我想在C#中制作一个级别和每个级别的游戏,以获得更多的对象。

我有这段代码:

Rectangle character, plus10;

Rectangle[] eraser, compass;


private void Initialize()
{            
    character = new Rectangle(_x, (int)_y, 40, 40);
    for (int i = 1; i <= objects; i++)
    {
        eraser[i] = new Rectangle(_m[i], 400, 40, 40);//I get the error here
        compass[i] = new Rectangle(_m[i], 400, 40, 40);//And here
    }
    plus10 = new Rectangle(_n, 400, 40, 40);
}


private void Form1_Paint(object sender, PaintEventArgs e)
{
        e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.background), 0, 0);
        if (_plrPos == playerPosition.Up)
        {
            e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.jump), _x, _y);
        }
        else if (_plrPos == playerPosition.Down)
        {
            e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.slide), _x, _y);
        }
        else
        {
            if (runImg == 1)
            {
                e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.run1), _x, _y);
                runImg = 2;
            }
            else if (runImg == 2)
            {
                e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.run2), _x, _y);
                runImg = 1;
            }
        }
        for (int i = 1; i <= objects; i++)
        {
            if (_obj[i] % 2 == 0)
            {
                e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.eraser), _m[i], 400);
            }
            else if (_obj[i] % 2 == 1)
            {
                e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources.compass), _m[i], 400);
            }
        }
        e.Graphics.DrawImage(new Bitmap(Happy_Math.Properties.Resources._10), _n, 400);

        Initialize();
    }

我收到此错误:

System.NullReferenceException: Object reference not set to an instance of an object.
  at Happy_Math.Form1.Initialize() in C:\Users\Dany\Documents\Visual Studio   2010\Projects\Happy Math\Happy Math\Form1.cs:line 68
  at Happy_Math.Form1.Form1_Paint(Object sender, PaintEventArgs e) in C:\Users\Dany\Documents\Visual Studio 2010\Projects\Happy Math\Happy Math\Form1.cs:line 112
  at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
  at System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
  at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
  at System.Windows.Forms.Control.WmPaint(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
  at System.Windows.Forms.Form.WndProc(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

您应该在定义数组时或在Initialize方法内部初始化数组:

Rectangle[] eraser = new Rectangle[objects.Length];
Rectangle[] compass = new Rectangle[objects.Length];

如果您不知道元素计数使用List<T>

答案 1 :(得分:0)

几乎不可能“猜测”解决方案,但这看起来很可疑:

for (int i = 1; i <= objects; i++)

它不应该是零基础吗?像这样:

for (int i = 0; i < objects; i++)

你有两个。 也许你习惯了另一种语言,没问题。请记住,在C#数组中,索引是从零开始的。