0

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

Giulio Caccin
  • 2,554
  • 6
  • 30
  • 47
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun Mar 30 '21 at 10:03
  • one of the possible answers is that the array is not initialized but I'm pretty sure I did that in line 11. In every example I look at online, it's the same thing. – Alex Antoine Mar 30 '21 at 10:09

3 Answers3

0

At line 11 you init an array with length 2 but is is still empty to fill it add

csa[0] = new CurryStudent();
csa[1] = new CurryStudent();
Connor Stoop
  • 719
  • 5
  • 18
0

You create a list of CarryStudent[] csa. Then you ask for the first element in that list. But your first element in that list is nothing yet, you did not populate your list with elements of type CarryStudent. Thus the exception

vadim3108
  • 1
  • 1
0

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();
Prasad Telkikar
  • 10,243
  • 2
  • 12
  • 31