尝试初始化内部类数组时的NPE

时间:2017-03-14 11:25:59

标签: c# class computer-science internal

我正在尝试在类中创建一个内部类,然后将其初始化为第一个值= 0 我有这个。

class Vehicle {
         internal class AGVSteps {
             public double X { get; set; }
             public double Y { get; set; }
         }

         private AGVSteps[] steps ;
         public AGVSteps [] Steps {
             get { return this.steps; }
         }

        public Vehicle() { //constructor
            this.steps = new AGVSteps[2000];
            this.steps[0].X = 0; //CRASHES HERE
            MessageBox.Show(this.steps[0].X + "");
            for (int i = 0; i < 2000; i++) {
               // this.steps[i].X = -1;
                //this.steps[i].Y = -1;
            }
        }

}

任何想法?我收到NPE错误。谢谢!

1 个答案:

答案 0 :(得分:1)

stepsAGVSteps个对象的容器。您需要初始化AGVSteps本身

this.steps = new AGVSteps[2000];
for (int i = 0; i < steps.Length; i++) {
    steps[i] = new AGVSteps();
}