I'm trying to initialize a string list to use in a later method. I don't know what size the list needs to be because the data sets that will be passed to the program are not consistently sized. Because of this, I'm dynamically adding items to the list. However, when I initialize the list in the space above what would be considered main()
like this,
List<string> studentData;
and call it in a public method like this,
studentData.AddRange(student); // Student is an array
I get an exception that says,
System.NullReferenceException: 'Object reference not set to an instance of an object' studentData was null.
but earlier in my code I called initialized a different list like this and it worked just fine,
List<string> fileInput; // Holds comma delimited file text
.
.
.
fileInput = text.Split(',').ToList();`
So then I went to DotNetPerls to figure out how to initialize a non empty list. Per the code examples this should have worked,
List<string> studentData = new List<string>();
studentData.Add("Thing");
but studentData
got green underlined with warning text
studentData is never assigned to, and will always have its default value null.
and studentData.Add("Thing");
got red underlined with error text
The name 'studentData.Add' doesn't exist in the current context. The name 'studentData' doesn't exist in the current context.
and ("Thing")
was handled separately as well.
Type expected.
Tuple must contain at least two elements. ) expected. Invalid token '"Thing"' in class, struct, or interface member declaration
I'm still fairly new to C# and none these errors makes sense to me. What am I doing wrong here?