0
public class Teacher{
    public string imageUrl;
    public TeacherEducationalQualification[] teacherEducationalQualification;
}

public class TeacherEducationalQualification{
    public string NameOfDegree;
    public string NameOfUniversity;
    public int YearOfGraduation;
}

In the above codes when I instantiate Teacher class like

Teacher teacher= new Teacher();

This works fine but when I instantiate array in Teacher class for object 'teacher'

teacher.teacherEducationalQualification = new TeacherEducationalQualification[5];

It gives me an error 'Object reference not set to an instance of an object' whenever i try to access any variable to set values in it.

teacher.teacherEducationalQualification[1].NameOfDegree= "abc";

Please Help.

3 Answers3

1

After you initialize an array of objects (reference type) its items will be null. You have to iterate (loop) through the array and initialize each item.

If you want to set each item individually however, you can do something like this

Teacher teacher = new Teacher();

teacher.teacherEducationalQualification = new TeacherEducationalQualification[5];

// Initialize item at index 0; indices start with 0 so the 1st item has index 0
teacher.teacherEducationalQualification[0] = new TeacherEducationalQualification();
teacher.teacherEducationalQualification[0].NameOfDegree= "abc";

// Initialize item at index 1

// Initialize item at index 2

// Initialize item at index 3

// Initialize item at index 4; this is the last index, your 5th item
teacher.teacherEducationalQualification[4] = new TeacherEducationalQualification();
teacher.teacherEducationalQualification[4].NameOfDegree= "xyz";

// Or in a different way with the help of a local variable
var qualification;

qualification = new TeacherEducationalQualification();
qualification.NameOfDegree= "abc";
// set other fields
teacher.teacherEducationalQualification[0] = qualification;

// ...

qualification = new TeacherEducationalQualification();
qualification.NameOfDegree= "xyz";
// set other fields
teacher.teacherEducationalQualification[4] = qualification; // last item

Note: field names in C# should be camelCase - start with a lowercase letter

public string nameOfDegree;

Properties, on the other hand, should be PascalCase - start with an uppercase letter

public string NameOfDegree { get; set; } // auto-implemented property
Ma3x
  • 5,761
  • 2
  • 17
  • 22
1

When you instantiate an array you are essentially creating a data structure which is capable of holding a number of objects next to each other, however the objects (i.e., the array elements) must be instantiated separately. When an array is created, all elements of the array are initialised with the default value of the array type. For example for an array of integers all elements would be 0, for an array of DateTime all elements would be DateTime.MinValue and for an array of any reference type (like your example above) the elements will be null. That's why you got a NullReferenceException. If you like to instantiate an array as well as all elements using the default constructor you can use the following:

teacher.teacherEducationalQualification = new TeacherEducationalQualification[5];

for(int i = 0; i < teacher.teacherEducationalQualification.Length; i++)
    teacher.teacherEducationalQualification[i] = new TeacherEducationalQualification();

After that, it'll be safe to assign to properties of each array element. My answer above does not necessarily mean this is the best design to solve this problem though.

Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
1

When you instantiate a array of objects in c# you instantiate an array with null values:

teacher.teacherEducationalQualification = new TeacherEducationalQualification[5];

equals to

teacher.teacherEducationalQualification = new TeacherEducationalQualification[]{null, null, null, null, null};

so

teacher.teacherEducationalQualification[1] == null

you must instantiate the object before use it:

teacher.teacherEducationalQualification[1] = new TeacherEducationalQualification();
teacher.teacherEducationalQualification[1].NameOfDegree= "abc"

Otherwise if you don't want to create instances of Object, you need to use struct:

public struct TeacherEducationalQualification{
    public string NameOfDegree;
    public string NameOfUniversity;
    public int YearOfGraduation;
}
Stefano Balzarotti
  • 1,760
  • 1
  • 18
  • 35