-1

I am a very new at coding, just my first steps so I apologize for not saying something properly technically. I try to call Array2 from Structure Student on Array1 from Structure Class.

The last two lines of code don't work, I get the "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object."

How can I correct that? I feel that I am missing something insignificant but still can't figure it out. Thank you in advance for all answers!

class Program
{ 
    struct Class
    {
        public Student[] Array1;
    }
    struct Student
    {
        public string firstName;
        public string lastName;
        public int code;
        public GradesCourse[] Array2;
    }
    struct GradesCourse
    {
        public string nameCourse;
        public double gradeMidterm;
    }

        Class myClass;
        myClass.Array1 = new Student[2];
        Student myStudent;
        myStudent.Array2 = new GradesCourse[2];

        for (int i = 0; i < myClass.Array1.Length; i++)
        { 
            myClass.Array1[i].firstName = Console.ReadLine();
            myClass.Array1[i].lastName = Console.ReadLine();
            myClass.Array1[i].code = Convert.ToInt32(Console.ReadLine());
            
            for (int j = 0; j < myStudent.Array2.Length; j++)
            { 
                
                myClass.Array1[i].myStudent.Array2[j].nameCourse = Console.ReadLine();       
                myClass.Array1[i].myStudent.Array2[j].gradeMidterm = Convert.ToDouble(Console.ReadLine());
            }
            
        }
ilyar
  • 3
  • 1
  • which line in the code throws the error? – Chetan Dec 29 '20 at 15:42
  • myClass.Array1[i].myStudent.Array2[j].nameCourse = Console.ReadLine(); – ilyar Dec 29 '20 at 15:44
  • The last two lines shouldn't *compile*. A `Student` doesn't have a `myStudent` field/property. Please try to give us a [mcve], that is, some code that you've actually compiled and executed and know demonstrates the problem you want to ask about. – Damien_The_Unbeliever Dec 29 '20 at 15:49
  • 1
    None of these types make sense as structs. structs should be small, immutable, represent a single logical value, etc. These types should be classes. – Servy Dec 29 '20 at 16:02

1 Answers1

-1

First of all your code doesn't compile There is a .myStudent that shouldn't be there on the last two lines.

Because they are structs you shouldn't create a new myStudent variable at any part of the loop since they will not point to the same struct from myClass Take a look at this corrected code:

void Main()
{
    Class myClass;
    myClass.Array1 = new Student[2];
    
    for (int i = 0; i < myClass.Array1.Length; i++)
    {
        myClass.Array1[i].firstName = Console.ReadLine();
        myClass.Array1[i].lastName = Console.ReadLine();
        myClass.Array1[i].code = Convert.ToInt32(Console.ReadLine());
        myClass.Array1[i].Array2 = new GradesCourse[2];

        for (int j = 0; j < myClass.Array1[i].Array2.Length; j++)
        {
            myClass.Array1[i].Array2[j].nameCourse = Console.ReadLine();
            myClass.Array1[i].Array2[j].gradeMidterm = Convert.ToDouble(Console.ReadLine());
        }
    }
}
Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80