-1

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

enter image description here

I'm still fairly new to C# and none these errors makes sense to me. What am I doing wrong here?

AwesomeBob2341
  • 135
  • 2
  • 14
  • 1
    In the first example you try to call method from uninitialized object and in the second you assign value to uninitialized object, that's the diiference. – FCin Feb 28 '18 at 05:30
  • Have you tried making `studentData.AddRange(student);` into `studentData.AddRange(student.ToString());` ? – Shan Coralde Feb 28 '18 at 05:35
  • @ShanCoralde `studentData.AddRange(student.ToString());` will raise a conversion error if typed like that. I did try to use a string instead of an array, but `studentData` still says it's not assigned and I still get the original error. – AwesomeBob2341 Feb 28 '18 at 05:45
  • 1
    Have you tried @diberium's answer below? – Shan Coralde Feb 28 '18 at 05:52
  • I am sorry but you did not post the exact code to reproduce the last error message. Otherwise I would try to explain this also :) – Mong Zhu Feb 28 '18 at 06:27
  • I biffed it and grabbed the wrong screen shot. The one up now is the one I meant to grab. – AwesomeBob2341 Feb 28 '18 at 06:44
  • 1
    I could reproduce the token and context not found messages, which I explained in my edit. but the green line is not reproducible for me. Sometimes you get confounds from older built-version where the message was correct, but now you changed your code, the situation is different but the compiler still tells you that the situation persists. I really don't know – Mong Zhu Feb 28 '18 at 06:53
  • 1
    Well not really, I input the same code that's mentioned in the block quotes above the image and the image shows that. The error shows up after it tries to run. In any case though the explanation you provided and the answer from @diberium gets me further than where I was before. – AwesomeBob2341 Feb 28 '18 at 06:57

3 Answers3

5
List<string> studentData;

Just declare a variable studentData, its type is List<string>, and the value is null.

List<string> studentData = new List<string>();

This will create an empty list and assign to studentData.

TaW
  • 53,122
  • 8
  • 69
  • 111
diberium
  • 66
  • 3
1

I have tried the same, I didn't faced any issue. Please rebuild the solution and check once again.

List<string> studentData = new List<string>();
studentData.Add("Thing");
Ravikumar
  • 211
  • 1
  • 10
1

1)

when I initialize the list in the space above what would be considered main() like this,

List<string> studentData;

In this case you simply declare the variable but you have not initialized it yet. There is no value assigned to it. Hence the default value is null and you get this nice exception.

2)

List studentData = new List(); studentData.Add("Thing");

but studentData got green underlined with warning text

This error arrises when you declare studentData once on class level outside the scope of the method and once inside the method locally, and you choose the same name! So the compiler will favor the local variable and use this reference. The variable with the same name on class level (which is really a different variable although it has the same name) will not be used, hence your green line message

code picture

3)

The name 'studentData.Add' doesn't exist in the current context. The name 'studentData' doesn't exist in the current context.

This error arises when the compiler cannot find studentData at all! so that means that you have not even declared the variable either locally or on class level. (One tip here: if you declare it outside of main in a console application you need to make the variable static or create an instance of Program, otherwise you canno use the variable, since Main is static.)

EDIT:

Your posted screenshot paints a little different picture. This error message is misleading unfortunately. Actually the compiler wants to tell you that you cannot execute a method call like Add(..) outside of a method body! This is why you get the Compiler Error CS1519

The compiler now tries to infer what you might mean with this statement:

  • it thinks that you are trying to access a variable with the name studentData.Add which is not declared.
  • it thinks that you are trying to access a variable with the name studentData which is not declared.
  • it thinks that you are trying to access a variable with the name Add which is not declared.
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • the image you have attached isn't the way I tried to setup the initialization. I had `List studentData = new List(); studentData.Add("Thing");` in the class level and was getting that, but your explanation does makes sense. – AwesomeBob2341 Feb 28 '18 at 06:19
  • @rocky please post a screen shot of the way that you did showing the green line with the "studentData is never assigned to, and will always have its default value null." message – Mong Zhu Feb 28 '18 at 06:23
  • I went back and tried putting that error in the same way after fixing it, but it's not giving the green line anymore... – AwesomeBob2341 Feb 28 '18 at 06:34
  • @rocky now that is an entirely different picture! hold on a sec – Mong Zhu Feb 28 '18 at 06:39
  • I grabbed the wrong one I just fixed it. Sorry about that. – AwesomeBob2341 Feb 28 '18 at 06:41