IndexOutOfRangeException when index is less than the size of the collection

MattNorman

Well-known member
Joined
May 22, 2021
Messages
98
Programming Experience
1-3
I am getting an index out of range exception even though the index I am trying to access is within the bounds of the collection.

If I add a break point I see the size of the collection as 44 and it fail when trying to access the index 33. If I inspect the collection index 33 is present and contains all of the expected data.

C#:
double startTime = -1;
double endTime = -1;

for (int i = 0; i < data.Count; i++)
{
    indexTest = i;
    DataManagerLogs.WriteToErrorLog($"{classNamespace}.{methodName}", indexTest.ToString(), data.Count.ToString());
    if (i >= data.Count) { continue; }
    if (startTime == -1 && int.Parse(data[i].CallsOffered) > 0) { startTime = DataHelper.ReportSecondsFromString(data[i].Time); }
    if (int.Parse(data[i].CallsOffered) > 0) { endTime = DataHelper.ReportSecondsFromString(data[i].Time); }
}

I added some logging to see what the index was and the count at the time but this didn't help me to figure out the issue. It just shows the index incrementing and the count always remains at 44 until it hits index 33.
 
Solution
Either your system is broken or you're wrong because you obviously aren't going to get an IndexOutOfRangeException when the index isn't out of range. We can't really test your code to see if we can reproduce the issue because we don't have enough to compile it. I would suggest that you expand your code out so that every line is as simple as possible and find out exactly where the exception is thrown, then look at the stack trace to see whether it is actually occurring where you think it is or somewhere deeper. Maybe i is not even the index it's referring to.
Either your system is broken or you're wrong because you obviously aren't going to get an IndexOutOfRangeException when the index isn't out of range. We can't really test your code to see if we can reproduce the issue because we don't have enough to compile it. I would suggest that you expand your code out so that every line is as simple as possible and find out exactly where the exception is thrown, then look at the stack trace to see whether it is actually occurring where you think it is or somewhere deeper. Maybe i is not even the index it's referring to.
 
Solution
Either your system is broken or you're wrong because you obviously aren't going to get an IndexOutOfRangeException when the index isn't out of range. We can't really test your code to see if we can reproduce the issue because we don't have enough to compile it. I would suggest that you expand your code out so that every line is as simple as possible and find out exactly where the exception is thrown, then look at the stack trace to see whether it is actually occurring where you think it is or somewhere deeper. Maybe i is not even the index it's referring to.
You were correct. Looking into the stack trace further it was the DataHelper.ReportTimeFromSeconds method that was throwing the exception as I was passing a two part time string instead of the three part that it was splitting
 
It sounds like you might not have implemented that method as well as you might. It's a bit OT but could you post that method so we can take a look? I suspect that you are splitting a string and converting a substring to a number when you should be parsing the string as a DateTime or TimeSpan and then getting the number from the appropriate property.
 
Back
Top Bottom