-3

This is my code:

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)
    {

    }
}

}

I get the following error when I try to run:

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

It points to the b[0] = 0; line.

AlG
  • 14,697
  • 4
  • 41
  • 54
Hadya
  • 35
  • 3
  • 2
    http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – SLaks Mar 21 '14 at 16:25
  • See this question: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – tnw Mar 21 '14 at 16:26

4 Answers4

2

When you create your "Box" array, you are just creating an array of references. You then need to create the actual objects:

b[0] = new Box();
b[0].x = 0;
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

You have array of Box but did not create the Box object actually. You have to create object of Box class before you access its properties.

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

The exception that is thrown when there is an attempt to dereference a null object reference, MSDN.

Adil
  • 146,340
  • 25
  • 209
  • 204
1

Even though you are setting b to have a value in the line b = new Box[3], you never assign an actual value to b[0] so when trying to access b[0].x to assign to it, you are referncing a null value at that position in the array. Before the line that throws the error, insert a line similar to the following:

b[0] = new Box();

or use whatever constructor is appropriate for the Box class.

You could also automatically fill the complete array with items to start out with, so that the following lines referencing b[1] and b[2] do not throw the same error:

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

What you have done here is initialized an array, but filled it with null values. If you do not instantiate a type of class Box inside the array, you can not access any properties inside it. Before accessing x and y, you must try this:

b[0] = new Box();
b[1] = new Box();
b[2] = new Box();
Jason Higgins
  • 1,516
  • 1
  • 17
  • 37