-1

For school we are trying to draw a car in a windows form application, it needs to be OOP. We have 4 buttons in the form, one to draw a circle, one to draw a square, one to draw the complete car and one to make the car ride. We make a Car class which makes two circles and a square which is supposed to make the shape of a car. When we instantiate a new circle in the car class, we get a System.NullReferenceException error. How do we fix this? Button 3 is the create car class. Here is our code:

This is our form:

namespace Maissan_Autootje_V1
{
    public partial class Form1 : Form
    {
        Graphics g;
        public Form1()
        {
            InitializeComponent();
            g = this.CreateGraphics();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Refresh();
            Square square = new Square(150, 200, 150, 100);
            square.Draw(g);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Refresh();
            Circle circle = new Circle(100, 250, 100, 100);
            Circle circle2 = new Circle(280, 250, 100, 100);
            circle.Draw(g);
            circle2.Draw(g);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Auto auto = new Auto();
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

    }
}

This is our circle (the square is pretty much the same as the circle class):

namespace Maissan_Autootje_V1
{
    public class Circle
    {
        public Pen myPen = new Pen(Color.Blue, 5);

        int x;
        int y;
        int width;
        int height;

        public Circle(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public void Draw(Graphics g)
        {                      
                g.DrawEllipse(myPen, x, y, width, height);            
        }
    }
}

And this is supposed to be the class that draws the car:

namespace Maissan_Autootje_V1
{
    class Auto : Form1
    {
        Graphics g;

        public Auto()
        {


            Circle circle = new Circle(100, 250, 100, 100);
            Circle circle2 = new Circle(280, 250, 100, 100);
            Square square = new Square(150, 200, 150, 100);
            circle.Draw(g);
            circle2.Draw(g);
            square.Draw(g);
        }
    }
}

Thanks in advance!

Markinson
  • 2,077
  • 4
  • 28
  • 56
  • 1
    `g` is null, you need to set it to the graphic pane you are trying to draw on to.. better yet, pass the car into the form – Sayse Nov 03 '14 at 08:21

1 Answers1

1

You need to give the Auto an instance of the Graphics object.

Graphics g == null

So.. for example:

class Auto
{
    public Auto(Graphics g)
    {
        Circle circle = new Circle(100, 250, 100, 100);
        Circle circle2 = new Circle(280, 250, 100, 100);
        Square square = new Square(150, 200, 150, 100);
        circle.Draw(g);
        circle2.Draw(g);
        square.Draw(g);
    }
}
Rotem
  • 21,452
  • 6
  • 62
  • 109
Arcturus
  • 26,677
  • 10
  • 92
  • 107