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.