错误:"对象引用未设置为对象的实例"

时间:2015-01-31 22:27:27

标签: c#

这是我的代码。我知道错误是常见的,但我似乎无法弄清楚为什么会出现此错误。我试图从仪器阵列中的单词中删除元音。我做了一个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);

1 个答案:

答案 0 :(得分:1)

你的问题非常有趣 - 看看这一行:

  if (InstrumentCurrent != null);

最后删除分号,然后重试。