C#C
C#4y ago
16 replies
surwren

System.NullReference Exception Adding to a Dictionary [Answered]

I'm really confused why this is happening.

The dictionary in question is an attribute of a class that I've created.

Lecturer class:
public class Lecturer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Username { get; set; }

        public Dictionary<string, string> CourseLecturers { get; set; }

   
    }


I definitely do instantiate a new Lecturer object:
Lecturer lecturer = new Lecturer();


However, it refuses to assign the values taken from the SQL reader into the lecturer's dictionary attribute (CourseLecturers)

In the images attached, you can see that the reader successfully assigns the relevant values from the DB table into the variables, but the variables cannot be assigned to the dictionary for some reason. This is driving me insane, I've been debugging for 6 hours
nullReferenceError000.png
NullReferenceError001.png
NullReferenceError002.png
Was this page helpful?