1

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);
5
  • What does the array declaration look like? Commented Apr 4, 2009 at 0:01
  • Can you include the declaration of Items? Commented Apr 4, 2009 at 0:01
  • I've added some stuff to post Commented Apr 4, 2009 at 0:05
  • line 119 is (Console.WriteLine) ? Commented Apr 4, 2009 at 0:10
  • If it's short enough, can we see the input file? Commented Apr 4, 2009 at 0:14

5 Answers 5

6

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

There are two columns in the array.
0

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.

Comments

0

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]

3 Comments

I do not really understand what you mean in here.. What "Item" ??
sorry, it's Items (the 2d array you have)
The second dimension is large enough, it's the first dimension index that gives the exception. Items.GetLength(0) < Items.Length.
0
    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.

1 Comment

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

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.