简体   繁体   English

C#“对象引用未设置为对象的实例。”

[英]C# “Object reference not set to an instance of an object.”

I get that error when I attempt to use the btn connected to it: 当我尝试使用与其连接的btn时收到该错误:

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. pStatus是我在提高速度之前和之后使用并更新当前速度的面板。 plane is defined above as : plane在以上定义为:

Airplane plane = new Airplane();

The error seems to happen when it gets to plane.Accelerate(); 该错误似乎是在plane.Accelerate();上发生的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. 根据VS告诉我的内容,第一行if(PlanePosition.speed < Position.MAX_SPEED)一直在发生。


//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

Then PlanePosition is clearly null . 那么PlanePosition显然为null You are probably missing a 您可能错过了

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

in your constructor for Airplane or Airplane的构造函数中

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 . 因此,它接收默认值,该默认值对于引用类型为null You need to initialize this in the constructor: 您需要在构造函数中对此进行初始化:

public Airplane() {
    this.PlanePosition = new Position(// parameters for constructor);
    // rest of constructor
}

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是类的名称,您将需要实例化该类,然后将该方法与该对象一起使用。

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? 什么是PlanePosition? A class or an object? 类还是对象?

PlanePosition isn't being initialized. PlanePosition尚未初始化。 You need to make sure an object is assigned to PlanePosition before calling Accelerate 您需要在调用Accelerate之前确保将对象分配给PlanePosition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM