-1

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;
    }
}
Sandro
  • 467
  • 1
  • 5
  • 15
  • 2
    This answers 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) – Ňɏssa Pøngjǣrdenlarp Jul 14 '20 at 15:50
  • `students = new Student[5];` assigns a 5-element array of `Student`s to `students` but does not actually create any `Student` instances. Instead, every element of `students` is `null` until you make it otherwise. You need to do the same thing you did with `Grades`: assign some instances to its elements. – Lance U. Matthews Jul 14 '20 at 15:56
  • One way I learned to fix NullReferenceEx is by stepping through the code with a breakpoint and seeing what is null. then once you find what is null set a condition to prevent it from being null or skipping it if is null depending on what you want your code to do. – MrLu Jul 14 '20 at 15:58

1 Answers1

0

You're using the same index (i) for the student and the student's grade. What you really need is a nested loop, one to iterate through the students, and an inner one to loop through that student's grades:

for (int i = 0; i < students.Length; ++i)
{
    for (int j = 0; j < students[i].Grades.Length; ++j)
    {
        Console.Write(" {0}: ", students[i].Grades[j].Subject);
    }
}

Or you can use foreach loops which are arguably cleaner:

foreach (var student in students)
{
    foreach (var grade in student.Grades)
    {
        Console.Write(" {0}: ", grade.Subject);
    }
}

You also need to actually create Student objects in your student array. You can't just instantiate the array itself or all of the elements will be null.

public Student[] students = new Student[5];  // Array of 5 null elements
for (int i = 0; i < students.Length; ++i)
    students[i] = new Student();
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • Thanks! This make it iterate through grades five times though. I only want Art, Geography and Math to be printed one time. https://i.ibb.co/ZMLjtnX/fdf.png – Sandro Jul 14 '20 at 16:14
  • @Sandro You have 5 students. If each student has a grade for each of those subjects, how can you only show each subject once? What is the actual end result you're looking for? If you just want 1 student, then change the `new Student[5]` to `new Student[1]`. – itsme86 Jul 14 '20 at 16:17
  • 1
    Yes you're right about that. I might need to rethink this. Cause for every student I create the grades will be displayed just as many times which is wrong... Hmm. – Sandro Jul 14 '20 at 16:26