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.

4

6 回答 6

1

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

于 2013-05-24T18:34:17.173 回答
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++)
于 2013-05-24T18:34:39.813 回答
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.

于 2013-05-24T18:34:06.010 回答
0
if (... && (chArray[i + 1] == ' '))

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

于 2013-05-24T18:34:23.547 回答
0

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

于 2013-05-24T18:34:38.810 回答
0

C# 中的数组索引从 0 开始,数组的 Length 值从 1 开始计数,因此一个简单的解决方法是更改​​:

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

为了:

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

还有其他平台,如 Matlab,索引数组从 1 开始计数,因此,您的代码可能会成功!干杯!!!

于 2013-05-24T18:43:47.167 回答