0

I get that error when I attempt to use the btn connected to it:

private void btnAccel_Click(object sender, EventArgs e)
            {

                pStatus.Text = plane.speed.ToString();
                plane.speed = double.Parse(txtSpeed.Text);
                plane.Accelerate();
                pStatus.Text = plane.speed.ToString();   
            }

pStatus is a panel I use and update the current speed before and after I increase the speed. plane is defined above as :

Airplane plane = new Airplane();

The error seems to happen when it gets to plane.Accelerate();

public void Accelerate()
        {
            // increase the speed of the airplane

            if (PlanePosition.speed < Position.MAX_SPEED)
            {
                PlanePosition.speed = PlanePosition.speed + 1;  // or speed += 1;
            }//end of if
            numberCreated++;  // increment the numberCreated each time an Airplane object is created

        }//end of public Accelerate()

That first line if(PlanePosition.speed < Position.MAX_SPEED) is where it keeps happening from what VS is telling me.


//private variables
        private string name{get; set;}
       private Position planePosition;
        private static int numberCreated;

        //default constructor
        public Airplane()
        {

        }//end of public Airplane


        public Position PlanePosition{get;set;}

class Position
    {
        //private variables
     internal int x_coordinate;
     internal int y_coordinate;
     internal double speed;
     internal int direction;
     internal const int MAX_SPEED = 50;

        //default constructor
        public Position()
        {

        }//end of public Position

        public string displayPosition()
        {
            return "okay";
        }//end of public string displayPosition()
    }//end of class Position
4
  • Seems like you didn't initialize the PlanePosition field/property. Commented Jan 20, 2011 at 16:01
  • You should probably post the code for PlanePosition and Position Commented Jan 20, 2011 at 16:01
  • Is PlanePosition or Position a reference to a class? If so, make sure you have instantiated it, just like with your Airplane class. Commented Jan 20, 2011 at 16:03
  • So what are 'PlanePosition' and 'Position' and what does the debugger think they are? Commented Jan 20, 2011 at 16:03

3 Answers 3

1

Then PlanePosition is clearly null. You are probably missing a

PlanePosition = new Position(); // or whatever the type of PlanePosition is

in your constructor for Airplane or

private PlanePosition = new Position();

to initialize the field or analogously if it's a property.

I see you left the following comment to another answer:

public Position PlanePosition{get;set;}

So this is an automatic property, and you aren't initializing it. Therefore, it receives the default value which for a reference type is null. You need to initialize this in the constructor:

public Airplane() {
    this.PlanePosition = new Position(// parameters for constructor);
    // rest of constructor
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :D I'll be back i'm sure. ^_^'
0

Generally speaking, the error will occur when you try to use an object that you haven't instantiated.

So PlanePosition is the the name of the class, you'll want to instantiate the class and then use the method with the object.

PlanePosition myPlane = new PlanePosition();
myPlane.speed < ...

But I don't think there's enough detail provided to be any more specific than what I've given you. What is PlanePosition? A class or an object?

2 Comments

"public Position PlanePosition{get;set;}" Position is another class which holds the speed variable. And I think the right word to use is this is "composition?" is at least what the teacher calls it.
@allthosemiles: That's an automatic property, it's initialized to null by default so you need to initialize it in your constructor. And yes, that is composition. Please see my answer here: stackoverflow.com/questions/4749364/….
0

PlanePosition isn't being initialized. You need to make sure an object is assigned to PlanePosition before calling Accelerate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.