我创建了一个自定义类,如果仅实例化一个可以调用类里的方法,但如果实例化一个类的数组就不能。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.TransForm(1, 2);
Student[] students = new Student[2];
for (int i = 0; i < 2; i++)
{
students[i].TransForm(1, 2);
}
Console.ReadKey();
}
}
class Student
{
public double[] Position = { 0, 0 };
public void TransForm(double x, double y )
{
double[] position = { x, y };
Position = position;
}
}
}
在运行到students[i].TransForm(1, 2);这一行时会报错
System.NullReferenceException:“未将对象引用设置到对象的实例。”
我应该怎么办?