I'm trying to iterate through the array of Grades which is initiated in the students object but get 'Object reference not set to an instance of an object'. I believed the following line for (int i = 0; i < (students[i].Grades.Length); i++)
only would run three times (size of Grades array) then terminate, but it seems like its aiming to fulfill the iteration of students?:
If so, I'm kind of lost and wonder how to reach the field of the Subject instead of using this kind of iteration? Might add I'm a newbie so any advice is much appreciated!
// Iteration problems
for (int i = 0; i < (students[i].Grades.Length); i++)
{
Console.Write(" {0}: ", students[i].Grades[i].Subject);
}
Parts of the code in larger context:
class Program
{
static void Main(string[] args)
{
public Student[] students = new Student[] { };
students = new Student[5];
// Iteration problems
for (int i = 0; i < (students[i].Grades.Length); i++)
{
Console.Write(" {0}: ", students[i].Grades[i].Subject);
}
}
}
class Student
{
public GradeSubject[] Grades { get; set; } = new GradeSubject[]
{
new GradeSubject("Art"),
new GradeSubject("Geography"),
new GradeSubject("Math"),
};
}
public class GradeSubject
{
private string subject;
private char letter;
// Properties
public string Subject { get { return subject; } set { subject = value; } }
public char Letter { get { return letter; } set { letter = value; } }
public GradeSubject(string _subject)
{
subject = _subject;
}
}