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


i have code , when executes, throws indexoutofrangeexception, saying,

index outside bounds of array.

what mean, , can it?

depending on classes used can argumentoutofrangeexception

an exception of type 'system.argumentoutofrangeexception' occurred in mscorlib.dll not handled in user code additional information: index out of range. must non-negative , less size of collection.

what it?

this exception means you're trying access collection item index, using invalid index. index invalid when it's lower collection's lower bound or greater or equal number of elements contains.

when thrown

given array declared as:

byte[] array = new byte[4]; 

you can access array 0 3, values outside range cause indexoutofrangeexception thrown. remember when create , access array.

array length
in c#, usually, arrays 0-based. means first element has index 0 , last element has index length - 1 (where length total number of items in array) code doesn't work:

array[array.length] = 0; 

moreover please note if have multidimensional array can't use array.length both dimension, have use array.getlength():

int[,] data = new int[10, 5]; (int i=0; < data.getlength(0); ++i) {     (int j=0; j < data.getlength(1); ++j) {         data[i, j] = 1;     } } 

upper bound not inclusive
in following example create raw bidimensional array of color. each item represents pixel, indices (0, 0) (imagewidth - 1, imageheight - 1).

color[,] pixels = new color[imagewidth, imageheight]; (int x = 0; x <= imagewidth; ++x) {     (int y = 0; y <= imageheight; ++y) {         pixels[x, y] = backgroundcolor;     } } 

this code fail because array 0-based , last (bottom-right) pixel in image pixels[imagewidth - 1, imageheight - 1]:

pixels[imagewidth, imageheight] = color.black; 

in scenario may argumentoutofrangeexception code (for example if you're using getpixel method on bitmap class).

arrays not grow
array fast. fast in linear search compared every other collection. because items contiguous in memory memory address can calculated (and increment addition). no need follow node list, simple math! pay limitation: can't grow, if need more elements need reallocate array (this may expansive if old items must copied new block). resize them array.resize<t>(), example adds new entry existing array:

array.resize(ref array, array.length + 1); 

don't forget valid indices 0 length - 1. if try assign item @ length you'll indexoutofrangeexception (this behavior may confuse if think may increase syntax similar insert method of other collections).

special arrays custom lower bound
first item in arrays has index 0. not true because can create array custom lower bound:

var array = array.createinstance(typeof(byte), new int[] { 4 }, new int[] { 1 }); 

in example array indices valid 1 4. of course upper bound cannot changed.

wrong arguments
if access array using unvalidated arguments (from user input or function user) may error:

private static string[] romannumbers =     new string[] { "i", "ii", "iii", "iv", "v" };  public static string romanize(int number) {     return romannumbers[number]; } 

unexpected results
exception may thrown reason too: convention many search functions return -1 (nullables has been introduced .net 2.0 , anyway it's well-known convention in use many years) if didn't find anything. let's imagine have array of objects comparable string. may think write code:

// items comparable string console.writeline("first item equals 'debug' '{0}'.",     myarray[array.indexof(myarray, "debug")]);  // arbitrary objects console.writeline("first item equals 'debug' '{0}'.",     myarray[array.findindex(myarray, x => x.type == "debug")]); 

this fail if no items in myarray satisfy search condition because array.indexof() return -1 , array access throw.

next example naive example calculate occurrences of given set of numbers (knowing maximum number , returning array item @ index 0 represents number 0, items @ index 1 represents number 1 , on):

static int[] countoccurences(int maximum, ienumerable<int> numbers) {     int[] result = new int[maximum + 1]; // includes 0      foreach (int number in numbers)         ++result[number];      return result; } 

of course it's pretty terrible implementation want show it'll fail negative numbers , numbers above maximum.

how applies list<t>?

same cases array - range of valid indexes - 0 (list's indexes start 0) list.count - accessing elements outside of range cause exception.

note list<t> throws argumentoutofrangeexception same cases arrays use indexoutofrangeexception.

unlike arrays, list<t> starts empty - trying access items of created list lead exception.

var list = new list<int>(); 

common case populate list indexing (similar dictionary<int, t>) cause exception:

list[0] = 42; // exception list.add(42); // correct 

idatareader , columns
imagine you're trying read data database code:

using (var connection = createconnection()) {     using (var command = connection.createcommand()) {         command.commandtext = "select mycolumn1, mycolumn2 mytable";          using (var reader = command.executereader()) {             while (reader.read()) {                 processdata(reader.getstring(2)); // throws!             }         }     } } 

getstring() throw indexoutofrangeexception because you're dataset has 2 columns you're trying value 3rd 1 (indices always 0-based).

please note behavior shared idatareader implementations (sqldatareader, oledbdatareader , on).

you can same exception if use idatareader overload of indexer operator takes column name , pass invalid column name.
suppose example have retrieved column named column1 try retrieve value of field with

 var data = dr["colum1"];  // missing n in column1. 

this happens because indexer operator implemented trying retrieve index of colum1 field doesn't exist. getordinal method throw exception when internal helper code returns -1 index of "colum1".

others
there (documented) case when exception thrown: if, in dataview, data column name being supplied dataviewsort property not valid.

how avoid

in examples let me assume, simplicity, arrays monodimensional , 0-based. if want strict (or your're developing library) may need replace 0 getlowerbound(0) , .length getupperbound(0) (of course if have parameters of type system.array, doesn't apply t[]). please note in case upper bound inclusive code:

for (int i=0; < array.length; ++i) { } 

should rewritten this:

for (int i=array.getlowerbound(0); <= array.getupperbound(0); ++i) { } 

please note not allowed (it'll throw invalidcastexception), that's why if parameters t[] you're safe custom lower bound arrays:

void foo<t>(t[] array) { }  void test() {     // throw invalidcastexception, cannot convert int32[] int32[*]     foo((int)array.createinstance(typeof(int), new int[] { 1 }, new int[] { 1 })); } 

validate parameters
if index comes parameter should validate them (throwing appropriate argumentexception or argumentoutofrangeexception). in next example wrong parameters may cause indexoutofrangeexception, users of function may expect because they're passing array it's not obvious. i'd suggest validate parameters public functions:

static void setrange<t>(t[] array, int from, int length, func<i, t> function) {     if (from < 0 || from>= array.length)         throw new argumentoutofrangeexception("from");      if (length < 0)         throw new argumentoutofrangeexception("length");      if (from + length > array.length)         throw new argumentexception("...");      (int i=from; < + length; ++i)         array[i] = function(i); } 

if function private may replace if logic debug.assert():

debug.assert(from >= 0 && < array.length); 

check object state
array index may not come directly parameter. may part of object state. in general practice validate object state (by , function parameters, if needed). can use debug.assert(), throw proper exception (more descriptive problem) or handle in example:

class table {     public int selectedindex { get; set; }     public row[] rows { get; set; }      public row selectedrow {         {             if (rows == null)                 throw new invalidoperationexception("...");              // no or wrong selection, here return null             // case (it may reason use property             // instead of direct access)             if (selectedindex < 0 || selectedindex >= rows.length)                 return null;              return rows[selectedindex];         } } 

validate return values
in 1 of previous examples directly used array.indexof() return value. if know may fail it's better handle case:

int index = myarray[array.indexof(myarray, "debug"); if (index != -1) { } else { } 

how debug

in opinion of questions, here on so, error can avoided. time spend write proper question (with small working example , small explanation) more time you'll need debug code. first of read eric lippert's blog post debugging of small programs, won't repeat words here it's absolutely must read.

you have source code, have exception message stack trace. go there, pick right line number , you'll see:

array[index] = newvalue; 

you found error, check how index increases. right? check how array allocated, coherent how index increases? right according specifiation? if answer yes these questions you'll find here on stackoverflow please first check yourself. you'll save own time!

a start point use assertions , validate inputs. may want use code contracts. when went wrong , can't figure out happens quick @ code have resort old friend: debugger. run application in debug inside visual studio (or favorite ide), you'll see line throws exception, array involved , index you're trying use. really, 99% of times you'll solve in few minutes.

if happens in production you'd better add assertions in incriminated code, won't see in code can't see (but can bet).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -