這是我的代碼。我知道這個錯誤很常見,但我仍然無法弄清楚爲什麼會出現這個錯誤。我試圖從樂器陣列中的單詞中刪除元音。我做了一個If語句來確保錯誤不是由Instrument [a]爲空造成的。錯誤:「未將對象引用設置爲對象的實例」
string[] Instruments = new String[7];
Instruments[0] = "hej";
Instruments[1] = "cello";
Instruments[2] = "guitar";
Instruments[3] = "violin";
Instruments[4] = "double bass";
Instruments[5] = "drums";
string[] Vowels = new String[9];
Vowels[0] = "e";
Vowels[1] = "y";
Vowels[2] = "u";
Vowels[3] = "i";
Vowels[4] = "o";
Vowels[5] = "å";
Vowels[6] = "a";
Vowels[7] = "æ";
Vowels[8] = "ø";
string message = "start: ";
for (int a = 0; a < Instruments.Length; a++)
{
string InstrumentCurrent = Instruments[a];
Console.WriteLine(Instruments[a]);
for (int i = 0; i < Vowels.Length; i++)
{
if (InstrumentCurrent != null);
{
//InstrurmentCurrent = InstrumentCurrent += ""; (If I uncomment this, the code works fine)
InstrumentCurrent = InstrumentCurrent.Replace(Vowels[i], "");
}
}
message += InstrumentCurrent;
message += ", ";
}
MessageBox.Show(message);
異常消息應該告訴你確切的行號。 – 2015-01-31 22:31:11
@MAV我認爲這可能是因爲該名稱可能不包含如此產生的元音,但它實際上是語法(請參閱下面的答案)。 – zaitsman 2015-01-31 22:32:10
您爲'Instruments'變量創建了7個字符串的數組,但只初始化了6個字符串... – 2015-01-31 22:32:57