如何修复此 "Object reference not set to an instance of an object" 错误?

How do I fix this "Object reference not set to an instance of an object" error?

我在 运行 这个程序时遇到 System.NullReferenceException 错误。
就是说

the object reference is not set to an instance of an object

但我很确定这正是我在第 11 行中所做的。非常感谢任何有关如何解决此问题的想法。

在第 11 行,您初始化了一个长度为 2 的数组,但它仍然是空的,无法填充它 add

csa[0] = new CurryStudent();
csa[1] = new CurryStudent();

您创建了一个 CarryStudent[] csa 列表。 然后您要求该列表中的第一个元素。 但是您在该列表中的第一个元素还没有,您没有使用 CarryStudent 类型的元素填充您的列表。因此异常

csa 是一个名为 CurryStudent 的用户定义 class 的实例数组。在第 11 行,您实例化了 CurryStudent class 的数组。在这里你忘了用它的元素填充一个数组,这些元素只是 CurryStudent class.

的一个实例

您需要使用 class CurryStudent 的实例实例化 csa 数组的每个元素。 喜欢,

for(int i = 0; i < csa.Length; i++)
    csa[i] = new CurryStudent();