0

I'm using these following classes to send to an object forward but somehow I'm getting an Null Reference Exception at "obj.course[0].age = 16;"

class Student{
int Id {get; set;}
List<Course> course {get; set;} 
}

class Course{
int age;
List<Subjects> subjects {get; set;}
}

class Subjects{
int history{get; set;}
int biology {get; set;}
int physics {get; set;}
}

//--------------------------------------------------

Student obj = new Student();
obj.Id = 1;
obj.course[0].age = 16;
obj.course[0].Subjects[0].history= 30;
obj.course[0].Subjects[0].biology = 45;
obj.course[0].Subjects[0].physics = 55;
Agony
  • 39
  • 6

1 Answers1

0

Add a constructor to the student class that initializes the course member in the student object.

public Student() 
{  
    course = new List<Course>();  
}

Alternatively, you could "new up" the course list after Student obj = new Student(); but it'll be easier

Dortimer
  • 619
  • 8
  • 25