I’m having the following classes. While trying to set the values of the employee class with the following code, i`m getting the error message:Object reference not set to an instance of an object.
How can I solve it?
public class Employee
{
public Test[] test{ get; set; }
public Employee()
{
this.test[0].Name = "Tom";
this.test[0].age= 13;
}
}
public class Test
{
public string Name {get; set;}
public int Age {get; set;}
}
You need to create an instance of test variable which is an array of Test[] objects before assigning any values to them. When creating an instance you have to set the number of elements it will hold.
If you dont know the number of Test obejct the array will hold, consider using List or ArrayList
Edit. List Example: