Search code examples
c#object-reference

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


I am getting a System.NullReferenceException error when I run this program.
It is saying that

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

But I pretty sure that that's exactly what I am doing in line 11. Any ideas on how to fix this are very much appreciated.

Obejct Reference not set to an instance of an object


Solution

  • csa is an array of instances of a user-defined class called CurryStudent. On line number 11 you instantiated the array of CurryStudent class. Here you forgot to fill an array with its elements that are nothing but an instance of CurryStudent class.

    You need to instanciate each element of csa array with an instance of class CurryStudent. Like,

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