tags:

views:

362

answers:

5

Why does the following code throw an exception?

for (int i = 0; i <= Items.Length-1; i++)
{
    Console.WriteLine(Items[i,1]);
}

Exception:

System.IndexOutOfRangeException was unhandled
  Message="Index was outside the bounds of the array."
  Source="Es"
  StackTrace:
       at Es.Program.Main(String[] args) in C:\Users\Fero\Documents\Visual Studio 2005\Projects\Es\Es\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Declaration of Items:

Function which gets the array of strings:

static string[,] ReadFromFile(string filename, int rowsF)
{
    StreamReader SR;
    string S;
    string[] S_split;

    SR = File.OpenText(filename);
    S = SR.ReadLine();

    string[,] myItems = new String[rowsF, 2];
    int row_number = 0;
    while (S != null)
    {
        S_split = S.Split('"');
        //temp_items[row_number,0] = 
        myItems[row_number,0] = S_split[1];
        myItems[row_number,1] = S_split[2];

        row_number++;
        S = SR.ReadLine();
    }
    SR.Close();
    return myItems;
}

string[,] Items = ReadFromFile(myFile, rowsF);
+6  A: 

You have a straight two-dimensional array. Length gives you the total number of elements in the array, but you're using it to calculate the index for a single dimension. What you want is:

for (int i = 0; i < Items.GetLength(0); i++)
{
    Console.WriteLine(Items[i,1]);
}
Shog9
+2  A: 

Check the length of Items[i]. It appears to be a 2D array, and it's apparently not null, because you'd get a different exception for that, so it probably is just an empty array at Items[i], or only contains one item.

Check for:

Items[i] == null
Items[i].Length > 0

EDIT: Your additional code helped. When you split the string to initialize Items, for the item that's giving you trouble, check what you're storing at index 1. Other than that, I can't see a problem with it.

Chris Doggett
There are two columns in the array.
Skuta
A: 

It's throwing an exception because Items[i].Length is < 2

you need to check that (Items[i].Length >= 2) before trying to access Items[i,1]

Aziz
I do not really understand what you mean in here.. What "Item" ??
Skuta
sorry, it's Items (the 2d array you have)
Aziz
The second dimension is large enough, it's the first dimension index that gives the exception. Items.GetLength(0) < Items.Length.
Guffa
ya, I realized that from Shog9's answer, thanks :)
Aziz
A: 

From what I see, there's a small chance one of the lines doesn't follow the format you require. It looks like you're retrieving text based on Quote (") delimitation, and S_split[] might not always have those number of fields.

A blank line at the end of the file, for example, will trigger the exception.

Raymond Martineau
A: 
    S_split = S.Split('"');
    //temp_items[row_number,0] = 
    myItems[row_number,0] = S_split[1];
    myItems[row_number,1] = S_split[2];

Are you sure you don't want S_Split[0] & S_Split[1]?

Should see the input file.

Carra
no, I checked the values .. my .dat file contains "something" xxxxxx and I need to get something and xxxxx
Skuta