I have a Subject class and a Student class which contains a list of Subjects implemented as:
class Student
{
// 1 Properties and Fields
public List<Subject> Subjects;
// 2 Constructor
public Student()
{
Subjects = new List<Subject>();
}
// 3 Methods
public Subject SelectSubject(Subject subject)
{
if (Subjects.Contains(subject))
{
return subject;
}
return null;
}
}
In the SelectSubject method, I want to pass in a subject and check if that variable exists in the Subjects class. However, I am getting a
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Can someone please help me fix this?