我正在學習如何執行多維數組,並且在嘗試填充數組時遇到此引用錯誤。有人有主意嗎?多維數組對象引用未設置爲對象的實例
public static string[][] itemLines;
public static void readTxtFile()
{
try
{
string[] lines = new string[420];
using (StreamReader sr = new StreamReader(TextFileDirectory.fileDirectoryThree))
{
int counter = 0;
while (!sr.EndOfStream)
{
lines[counter] = sr.ReadLine(); //All lines are in an array index
counter++;
}
}
for (var i = 0; i < lines.Length; i++)
{
itemLines[i] = lines[i].Split('Ü'); //All lines are in multiplexed array
Console.WriteLine("Line " + i + "'s first value is: " + itemLines[i][0]);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
這是踢我的屁股。我似乎無法弄清楚這一點。
編輯:我找到了答案。我必須有一個計數器變量,並用它正確地聲明數組。
這行做u得到錯誤? –
itemLines [i] = lines [i] .Split('Ü'); //所有行都以複用數組形式 – randomnessthatsfunny
如果'lines [i]'爲空,那麼'Split'方法可能會拋出NRE。根據'StreamReader'讀取的文件內容,可能有空字符串。 –