Jamie Wright
Jamie Wright

Reputation: 1359

C# System.IndexOutOfRangeException

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.

Upvotes: 0

Views: 4820

Answers (6)

Daniel Conde Marin
Daniel Conde Marin

Reputation: 7742

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!!!

Upvotes: 0

qJake
qJake

Reputation: 17119

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++)

Upvotes: 1

Wolfgang Ziegler
Wolfgang Ziegler

Reputation: 1685

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

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124632

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

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

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

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

Upvotes: 1

Servy
Servy

Reputation: 203821

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.

Upvotes: 0

Related Questions