Questions tagged [indexoutofrangeexception]

This exception is thrown when you try to access an element of a collection with an index that is outside its current bounds.

The IndexOutOfRangeException is implemented on several platforms and frameworks, though most typically associated with Microsoft's .NET framework.

An IndexOutOfRangeException is thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array.

See also Troubleshooting Exceptions: System.IndexOutOfRangeException and What is IndexOutOfRangeException and how do I fix it?

393 questions
226
votes
5 answers

What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?

I have some code and when it executes, it throws a IndexOutOfRangeException, saying, Index was outside the bounds of the array. What does this mean, and what can I do about it? Depending on classes used it can also be…
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
28
votes
1 answer

What is an "index out of range" exception, and how do I fix it?

I'm getting one of the following errors: "Index was out of range. Must be non-negative and less than the size of the collection" "Insertion index was out of range. Must be non-negative and less than or equal to size." "Index was outside the bounds…
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
19
votes
5 answers

Why does List IndexOf allow out-of-range start index?

Why does List.IndexOf allow out-of-range start index? var list = new List() { 100 }; Console.WriteLine(list.IndexOf(1/*item*/, 1/*start index*/)); There will not be any exceptions. But there is no item with 1 index in this collection! There…
16
votes
3 answers

List Index Out of Range exception when creating a task

The exact error: Index was out of range. Must be non-negative and less than the size of the collection. I've index arrays and lists countless times. I've used for loops with arrays and lists countless times. The data is there, it works. Except…
14
votes
4 answers

Get string from array or set default value in a one liner

So we have ?? to parse its right-hand value for when the left hand is null. What is the equivalent for a string[]. For example string value = "One - Two" string firstValue = value.Split('-')[0] ?? string.Empty; string secondValue =…
Roy Berris
  • 1,502
  • 1
  • 17
  • 40
14
votes
1 answer

Why "Index was out of range" exception for List but not for arrays?

When I initialize an array and access elements using the indexer, that works just fine: object[] temp = new object[5]; temp[0] = "bar"; Now I would expect the same to work for a List, given you can initialize it by passing the capacity to the…
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
6
votes
1 answer

Golang panic: runtime error: index out of range only happens when run outside debugger

I have the following code used to find two integers that sum to a given total in a given slice: type Store_object struct { C int I int Prices []int } //..other unrelated functions... func FindItemPairs(scenarios…
jtgoen
  • 61
  • 1
  • 3
5
votes
10 answers

How to take Nested List as input in Python

I have to take input from the user in the following format and make a nested list from it. The first line is the number of rows. 3 Sourav Das 24 M Titan Das 23 M Gagan Das 22 F The nested list should be like : parentlist = [ ['Sourav',…
3
votes
2 answers

How to fix IndexOutOfRangeException in Unity

I have many image in folder screenshots. So this script will be calling the image from folder without applying. But the thing is, the image only displays one image. I mean they did not go to next image like slide show or fade in/out. Then I keep…
chimmy12
  • 29
  • 5
3
votes
1 answer

what to do when index is out of range when checking last index number?

Hello I'm new at python and I'm writing a module which should take a string as input and the output should be a list of each word, number or symbol with no white spaces. i.e (' 10 sweet apples') --> ['ten', 'sweet', 'apples']. To do so I have a…
3
votes
1 answer

Adding elements to ObservableCollection causes exception Index was out of range

I have a code that allows the user pick a file, adds some info about it to an observable collection and shows the upload progress, when finished, it binds the image to an Image view. It works properly at once but if you repeat the process, an…
3
votes
1 answer

Getting out of range exception when using while loop with vector in c++

I am getting an out of range exception with my while loop in my code. I have tried to use an Array instead and the error does not occur with the same loop structure. I am confused why this would happen. I believe that the savedValue <=…
ImLost
  • 31
  • 3
3
votes
1 answer

WinForms DataGridView throwing IndexOutOfRangeException

first timer here on Stack Overflow (although I've been lurking for ages). I'm developing a little application which contains two DataGridViews. The second DataGridView is filled via a binding on a list of a custom class objects (the user presses a…
3
votes
1 answer

uicollectionview inside uitableviewcell scroll will crash due to Index out of range

I learned the UICollectionView inside UITableViewCell from this tutorial. It works perfectly only when all of the collection views have the same numberOfItemsInSection, so it can scroll and won't cause Index out of range error, but if the collection…
3
votes
3 answers

Python "not in" index out of range enrror

I am dealing with some kind of problem and i could not find any solution. My problem is I am controlling a value in a nested list if it is not in another list and deleting it if it is not there but in the not in line it gives me an error like index…
user1726569
1
2 3
26 27