The reference to the object is not set in the instance of the object when using the <T> list in C #

I have the following code snippet that creates a compilation error:

public List<string> batchaddresses; public MapFiles(string [] addresses) { for (int i = 0; i < addresses.Count(); i++) { batchaddresses.AddRange(Directory.GetFiles(addresses[i], "*.esy")); } } 

I get an error when trying to use the List<T>.AddRange() method:

 Object reference not set to an instance of an object 

What am I doing wrong?

+4
source share
4 answers

Where are packet_data initialized?

Declaring a variable is not enough. You should initialize it like this:

 // In your constructor batchaddresses = new List<string>(); // Directly at declaration: public List<string> batchaddresses = new List<string>(); 
+12
source

you need to initialize the list

List<String> batchaddresses = new List<String>();

+1
source

The batchaddresses field batchaddresses not been initialized. You can initialize it as part of the declaration:

 public List<string> batchaddresses = new List<string>(); 
+1
source

From your fragment, batchaddresses does not seem to be initialized. Replace this line as follows:

 public List<string> batchaddresses = new List<string>(); 
0
source

Source: https://habr.com/ru/post/1332437/


All Articles