2011-03-15 74 views
1

我試圖讓列表的一維數組。我做這樣的:數組列表中拋出的NullReferenceException

public static List<string>[] words = new List<string>[30]; 
public static List<string>[] hints = new List<string>[30]; 

我這樣稱呼它:

foreach (string item in vars.directory) 
     { 
      reader2 = new StreamReader(item); 
      while (reader2.Peek() > 0) 
      { 
       string line = reader2.ReadLine(); 
       if (line.StartsWith("#")) 
       { 
        vars.words[counter].Add(line.Substring(1, line.Length - 1)); //here 
       } 
       else if (line.StartsWith("-")) 
       { 
        vars.hints[counter].Add(line.Substring(1, line.Length - 1)); //another here 
       } 
       else if (line == "@end") 
       { 
        counter++; 
       } 
      } 
     } 

我只是想補充一點,瓦爾在這裏我把我的公共變量和計數器的確是在0時循環開始。

編輯 在我匆忙我忘了補充問題......哎呀......

這就是:當我調用add函數(或任何與此有關的另一個功能),它返回一個空引用異常。我怎樣才能解決這個問題?

+5

問題是什麼? – 2011-03-15 19:56:04

+0

對不起...當我調用add函數時,它返回一個空引用異常。 – 2011-03-15 19:58:15

+0

見http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – 2011-03-15 20:27:59

回答

7

我假設你試圖調用.Add您的數組元素時崩潰。你需要用有效的對象初始化你的數組。

for(Int32 i = 0; i < vars.words.Length; ++i) 
    vars.words[i] = new List<string>(); 
for(Int32 i = 0; i < vars.hints.Length; ++i) 
    vars.hints[i] = new List<string>(); 
+1

由於'words'和'hints'是數組,你應該使用,而不是'Count'的'Length'財產方法。 – 2011-03-15 20:01:13

+0

我總是想知道這件事,是否有一種「正常」的方式來初始化所有的數組? – OopsUser 2011-03-15 20:01:21

+0

我把它和我得到:運算符'<'不能應用於類型'int'和'方法組'的操作數。我應該用長度替換計數嗎? – 2011-03-15 20:02:32

6

爲什麼不只是做一個List<List<string>>,但肯定可以讓列表

數組
0

你使用他們的權利之前,你可以初始化列表:

foreach (string item in vars.directory) 
{ 
    reader2 = new StreamReader(item); 
    while (reader2.Peek() > 0) 
    { 
     string line = reader2.ReadLine(); 
     // new code 
     if (vars.words[counter] == null) vars.words[counter] = new List<string>(); 
     if (vars.hints[counter] == null) vars.hints[counter] = new List<string>(); 

     if (line.StartsWith("#")) 
     { 
      vars.words[counter].Add(line.Substring(1, line.Length - 1)); //here 
     } 
     else if (line.StartsWith("-")) 
     { 
      vars.hints[counter].Add(line.Substring(1, line.Length - 1)); //another here 
     } 
     else if (line == "@end") 
     { 
      counter++; 
     } 
    } 
} 
+0

他可能有多個單詞或提示存儲在每個列表中,因此每個初始化程序只有在元素爲空時才需要運行。 – Avilo 2011-03-15 20:10:00

+0

@Avilo。接得好。我修復了它。 – 2011-03-15 21:46:39

0

這裏是一個一行來初始化大小30列表的數組:

static List<string>[] lists = (from i in Enumerable.Range(0, 30) 
            select new List<string>()).ToArray(); 
2

使用列表正如已經建議的那樣,列表會讓你擺脫你的問題,並且它比你的建築更加靈活和方便。

- > f.i.如果數據大小發生更改,則不必更改列表大小,但是陣列

0

問題是數組值被初始化爲默認值,而引用類型的默認值爲null

default(List<string>)回報null

所以,你需要在陣列中重新初始化對象之前,你可以訪問它們,否則你將得到一個NullReferenceException

一來初始化數組前面的所有對象的方法是使用LINQ的語句:

const int sizeOfLists = 5; 
List<string>[] lists = Enumerable.Range(0, sizeOfLists) 
    .Select(i => new List<string>()) 
    .ToArray(); 

另一種選擇是隻初始化並添加子列表,當你需要的時候,通過使用外部列表:

var lists = new List<List<string>>(); 
// ... 
var aSubList = new List<string>(); 
lists.Add(aSubList); 

這是特別有用的,如果你不知道外組名單上前面的大小,仍然是指數接近。

(這是一個註釋之前,但我做到了這一個答案,因爲很多其他的答案中得到了解決方案很感興趣,並沒有說明問題)

相關問題