My program errors out with a Null Reference Exception when it attempts to add to a List
Code for the for loop
for (int i = 0; i < UserCourses.Length; i++)
{
CurrentUser.Course_ID.Add(UserCourses[i]);
}
Code for CurrentUser (which is a new of type User)
public class User
{
public int coursenum;
public string Username;
public string Password;
public string FirstName;
public string LastName;
public string Email_Address;
public string User_Type;
public List<string> Course_ID;
public List<Course> Course;
}
I had it display the UserCourses[i] and it displayed successfully with the correct information, what am I doing wrong here?
You are not initializing the
Course_IDproperty to contain a reference to a newList<string>. So you are callingAddon a null reference.(Also, you might consider using the
AddRangemethod, which will add the whole array/list you are attempting to add with one line of code. This will eliminate the need to write your own loop.)