Can anybody suggest what is wrong here?
public class Student
{
public List<Class> Classes { get; set; }
public Student(List<Class> classes)
{
this.Classes = classes;
}
public Student(Class class)
{
//This does not work
//Error: Object reference not set to an instance of an object.
this.Classes.Add(class);
}
}
Calling it as following works
var classes = new List<Classes>();
classes.Add(new Class("English", "Elective"));
classes.Add(new Class("Math", "Core"));
..more classes to add
Student student = new Student(classes);
When I have call like this (only one class to add)
Student student = new Student(new Class("Masters","Accounts"));
I get error.
Thank you.
Your overload with Class needs to initialize Classes before adding class. Try this: