I always have coded in C++ but I am learning C# now. I got a trouble with accessing the object value.
Here is what I tried.
class Program
{
static void Main(string[] args)
{
StudentInfo[] student = new StudentInfo[2];
student[0] = new StudentInfo(100, 4);
student[1] = new StudentInfo(101, 3);
Trying ty = new Trying();
ty.trial(); /// I got trouble with this line
}
}
class Trying
{
StudentInfo[] student = new StudentInfo[2]; // I added this line since it prevents compiler error
public void trial()
{
Console.WriteLine(student[1].Gpa); // I got trouble with this line
}
}
class StudentInfo
{
public int studentNo {get; set;}
public int Gpa {get; set;}
public StudentInfo(int cc, int ct)
{
studentNo = cc;
Gpa = ct;
}
}
The error says
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
How can I access the value student[1].Gpa
correctly in the class Trying
?