-1

This is my code. I know the error is common but I still can't seem to figure out why this error appears. I am trying to remove the vowels from the words in the Instruments Array. I made an If-statement to make sure the error is not caused by Instrument[a]being null.

       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 Answers1

1

Your issue is very very interesting - look at this line:

  if (InstrumentCurrent != null);

Remove the semicolon at the end and try again.

zaitsman
  • 7,571
  • 4
  • 35
  • 64
  • It worked! thanks. But if I remove the entire if-statement I still get the error which does not make sense to me. – Christian Hinge Jan 31 '15 at 22:37
  • Well that is as per @JeffMercado in the comments - your array is of size 7 but you only initialized 6 members. So either add one more instrument or change `string[] Instruments = new String[6];` – zaitsman Jan 31 '15 at 22:38