0

Im having some problems trying to resolve this C# error:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at DayZMap.ProcessMemory.CutString(String mystring) in Z:\p\Memory.cs:line 45
   at DayZMap.Map.refreshMap(Object sender, PaintEventArgs e) in Z:\p\Form1.cs:line 517
   at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.OnPrint(PaintEventArgs e)
   at System.Windows.Forms.Control.WmPrintClient(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The function it is crashing on is:

public string CutString(string mystring)
{
    char[] chArray = mystring.ToCharArray();
    string str = "";
    for (int i = 0; i < mystring.Length; i++)
    {
        if ((chArray[i] == ' ') && (chArray[i + 1] == ' '))
        {
            return str;
        }
        if (chArray[i] == '\0')
        {
            return str;
        }
        str = str + chArray[i].ToString();
    }
    return mystring.TrimEnd(new char[] { '0' });
}

It throws the exception on the line:

if ((chArray[i] == ' ') && (chArray[i + 1] == ' '))

Any advice would be appreciated. Thanks.

1
  • when i = mystring.Length-1, i+1 = mystring.Length that is the problem. Commented May 24, 2013 at 18:33

6 Answers 6

1

It is very likely that when you are indexing chArray[i + 1], you are exceeding the size of the array.

Take, for example, if chArray has 5 characters, when i is 4 in your loop, it will attempt to access chArray[5] with your code, which is out of bounds (the bounds of the array in that example would be 0-4).

I don't know what you meant for this code to do, but one fix would be to limit your for by one less:

for (int i = 0; i < mystring.Length - 1; i++)
Sign up to request clarification or add additional context in comments.

Comments

1

chArray[i + 1] is outside the length of the array, you likely meant to iterate to mystring.Length - 1.

Comments

0

On the last iteration of your loop i is the largest valid index of that collection.

You try to access the item at index i + 1. That index doesn't exist.

You can loop up to the second-to-last valid index if you need to access the "next" index in the body of the loop.

Comments

0
if (... && (chArray[i + 1] == ' '))

When i == myString.Length - 1, that line goes one past the bounds of the string.

Comments

0

If the last character is ' ' you are indexing at an out of bounds positions with chArray[i + 1]

Comments

0

Array indexing in C# starts from 0, and the Length value of the array counts from 1, so a simple fix would be to change:

for (int i = 0; i < mystring.Length; i++)

for:

for (int i = 0; i < mystring.Length - 1; i++)

There are other platforms like Matlab, that indexes arrays counting from 1, so , in there your code could have success!. Cheers!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.