System.IndexOutOfRangeException - Index was outside the bounds of the array

Snuge picture Snuge · Apr 25, 2012 · Viewed 21.7k times · Source

I am creating a program that imports a 2d object array of information from an excel sheet. It then passes this array to ProcessObjects method to be processed and printed/exported back out to an excel template.Can anyone tell me why I'm getting this error message?

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Project.exe

Additional information: Index was outside the bounds of the array."

private void ProcessObjects(object[,] classesArray, object[,] classesAvailabilityArray, Excel.Workbook workbook2, Excel.Sheets excelSheets)
{
    // once classes are selected, they are copied to a temporary location
    // while they're waiting to be printed
    object[,] tempArray = new object[6,3];

    // This stops the while loop once enough credit hours have been taken 
    // It must reach 123 hours for CS Degree .
    int hourCounter = 0;

    int iteration = 0;

    while (hourCounter < 123)
        {
            // this while loop copies some classes from classesArray to tempArray
            // so they can be printed into the excel template (NewStudentTemplateCS.xlsx)
            //
            int classes = 0, hours = 0; // stops while loop if limit is reached
            int w = 0, x = 0; // used to select individual elements of tempArray (0 based)
                              // w = row
                              // x = column
            int y = 1, z = 1; // used to select individual elements of classesArray (1 based)
                              // y = row
                              // z = column
            while(classes < 7 || hours < 17)
            {
                // this loop checks the status of the flag and stops at the first avaliable
                // class/row of classesArray
                while (classesArray[y,7] == (object)1)
                {
                    y++;
                }

                // copies the call EX: "MATH 2313" from classesArray to tempArray
                tempArray[w,x] = classesArray[y,z];
                x += 2;
                z += 2;
                // copies the name EX: "Calculus I" from classesArray to tempArray
                tempArray[w, x] = classesArray[y, z];
                x++;
                z++;
                // Copies the hours EX: "3" from classesArray to tempArray
                tempArray[w, x] = classesArray[y, z];

                Console.WriteLine("debug test");

                // increments classes, hours, and hourCounter for exit decision
                classes += 1;
                hours += (int)classesArray[y, z];
                hourCounter += (int)classesArray[y, z];

                // sets flag to one
                z += 3;
                classesArray[y, z] = 1;

            }// end while loop

            // print method that prints temp array and clears tempArray for next use
            PrintArray(tempArray, iteration, workbook2, excelSheets);

            // iterates iteration
            iteration++;

        } // end while loop
        // print method that prints temp array and clears tempArray for next use
        PrintArray(tempArray, iteration, workbook2, excelSheets);

        // iterates iteration
        iteration++;

    } // end while loop
} // end ProcessObjects method

I have commented out each of the following lines individually, but each line of code all returns the same error I listed above.

Console.WriteLine("debug test");

// increments classes, hours, and hourCounter for exit decision
classes += 1;
hours += (int)classesArray[y, z];
hourCounter += (int)classesArray[y, z];

// sets flag to one
z += 3;
classesArray[y, z] = 1;

Answer

Dour High Arch picture Dour High Arch · Apr 25, 2012

Step through your code in the debugger:

object[,] tempArray = new object[6,3];

You are creating an array with max indexes at tempArray[5, 2]. Then you start looping. At the start of each loop:

int w = 0, x = 0;

Then in the body of the loop:

tempArray[w,x] = classesArray[y,z];

You assign to tempArray[0, 0]

x += 2;
z += 2;
tempArray[w, x] = classesArray[y, z];

You assign to tempArray[0, 2]

x++;
z++;
// Copies the hours EX: "3" from classesArray to tempArray
tempArray[w, x] = classesArray[y, z];

You assign to tempArray[0, 3]. But the maximum index of tempArray is [0, 2]; your array index is out of range, exactly what the exception was telling you.

If you can be certain that y and z can never go outside the bounds of classesArray, you can declare tempArray like this:

object[,] tempArray = new object[classesArray.GetLength(0), classesArray.GetLength(1)];

But with all those hard-coded magic numbers, and trying to synchronize arrays with differing bases, this is very risky code.