0

How can i add an item to a member of List<> array?
please see the example below :

List<string>[] array_of_lists = new List<string>[10];
array_of_lists[1].Add("some text here");

but a have the error below :

Object reference not set to an instance of an object.

what this error mean and how can i fix it?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results – Moo-Juice Oct 11 '13 at 11:02
  • What is `int.Parse(txt_ParaCount_In_LinkScanner.Text)`'s value? – Moo-Juice Oct 11 '13 at 11:06
  • You don't need to make a list an array, a list is already an array, just do List myList = new List –  Oct 11 '13 at 11:06
  • please see my edit. for some reasons i need array of arrays... – SilverLight Oct 11 '13 at 11:11
  • 1
    Do you really need an array of type List? Because that´s what you actually do, you build an array in which every entry itself is a list. For Twodimensional collection you´d rather use some kind of table or maybe a Dictionary as well... – MakePeaceGreatAgain Oct 11 '13 at 11:12
  • 1
    Use a `List>` – Sam Leach Oct 11 '13 at 11:12
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 13 '13 at 02:19

5 Answers5

7

You have initialized the array, but all elements are null yet. If you want to initialize it with a List<String> at a given index you can't use Add which is a method of List<T>.

In this way you initalize the array at the second element:

array_of_lists[1] = new List<string>{"some text here"};

Note also that indices start with 0, not 1.

Here's a demonstration.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

I think you mixed List<T> and arrays.

From MSDN

The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.

So, easyly you can write,

List<string> array_of_lists = new List<string>();
array_of_lists.Add("some text here");
SilverLight
  • 19,668
  • 65
  • 192
  • 300
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

problem is that when you initialize an array, it is created with default values of items. For most of the value types (int, float, vs... ) default value will be 0. for reference types (strings and nullable and List and many other) default value will be null.

so your code should be like this

List<string>[] list_lines_link_scanner_ar = new List<string>[int.Parse(txt_ParaCount_In_LinkScanner.Text)];

// this is the line -->
list_lines_link_scanner_ar[1] = new new List<string>();
//  <----
list_lines_link_scanner_ar[1].Add("some text here");
Erdogan Kurtur
  • 3,630
  • 21
  • 39
  • ok, that null issue was the problem. but should i do list_lines_link_scanner_ar[?] = new new List(); for every part of my array? – SilverLight Oct 11 '13 at 11:19
  • Yes, just use for or foreach. – FallenSquirrel Oct 11 '13 at 11:24
  • @MoonLight : yes you have to since every member of array is null. try using another list for lists and add them when you require them. and an array of lists is smelling. What exactly are you after? @FallenSquirrel : foreach? every item is already null, ìn `foreach `(var item in list_lines)`, item will always be null and foreach variable cannot be set. – Erdogan Kurtur Oct 11 '13 at 11:42
1

After so many edits, changes and commented answers I´d like to give a complete solution for you:

List<string>[] array_of_lists = new List<string>[10];
for (int i = 0; i < array_of_lists.Length; i++) {
    array_of_lists[i] = new List<string>();
    array_of_lists[i].Add("some text here");
    array_of_lists[i].Add("some other text here");
    array_of_lists[i].Add("and so on");
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
-1

Declare:

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

Add:

listOfList.Add(new List<string> { "s1", "s2", "s3" });

unless you really need an array.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73