0

I am really a beginner in c# and encountered this problem which I cannot solve even after spent hours searching the internet. I do not understand why the stack object Grades is always null and everytime i run the application, it throws an exception of "System.NullReferenceException". And when I hover over my mouse on Grades, it says "field 'Student.Grades' is never assigned to and will always have its default value null". I also cannot use "{get;set;}" to access this property. So can anyone tell me why? Many thanks!!!(First time asking question on stack overflow sorry for the bad formatting)

class Student : Person
{

    public int studentID { get; set; }
    public static int nextID;
    public Stack<int> Grades;

    public Student(string firstName, string lastName, DateTime birthDate, 
           string address1, string address2, string city, string state, 
           string zip, string country)
    {
        FirstName = firstName;
        LastName = lastName;
        BirthDate = birthDate;
        Address1 = address1;
        Address2 = address2;
        City = city;
        State = state;
        Zip = zip;
        Country = country;
        //track the number of students enrolled
        studentID = nextID;
        nextID++;
    }
}
Mohammad Akbari
  • 4,486
  • 6
  • 43
  • 74
mzpq01
  • 15
  • 6

1 Answers1

0

In your constructor you can add

this.Grades = new Stack();

code.

Class in c# are with null default value.

Dei Revoledo
  • 175
  • 1
  • 5