Questions tagged [indexoutofboundsexception]

An exception that occurs when you attempt to access an object that is outside the boundaries of the container. Common containers are arrays or array-based objects. This is a language-independent tag.

An IndexOutOfBoundsException is an exception that occurs when you attempt to access an object that is outside the boundaries of the container. Common containers are arrays or array-based objects.

IndexOutOfBoundsExceptions are generally caused by one of 2 things...

  1. Passing a negative pointer index
  2. Passing a pointer that is equal-to, or greater than the length of the container.

If we use the example of an array which is defined as being able to hold 4 objects...

Object[] myArray = new Object[4];

Arrays, and array-based containers, in most languages have zero-based references (See Wikipedia for a list of exceptions). This means that the first item in the array is myArray[0], not myArray[1]. For new programmers, this is often not obvious, and can result in them trying to reference objects from 1-4 rather than 0-3. This causes the programmer to receive an IndexOutOfBoundsException when trying to reference myArray[4].

A common way to make this mistake is to use a for loop as follows:

for (int i=0; i<=myArray.Length; i++)
    // do something with myArray[i];

This will fail with an IndexOutOfBoundsException because only elements [0] through [3] exist. myArray[myArray.Length] is myArray[4], which does not exist.

Similarly, trying to reference a negative index, such as myArray[-1] will fail, as it exists outside the bounds of the array. This would commonly occur when trying to obtain an index reference from another method, where the method is using a negative number to indicate a failure.

For example, assume the following code...

String myName = "Fred";
Object myObject = myArray[myName.getIndexOf("B")];

In this example, the getIndexOf("B") method will find the position of the letter B in the given String. However, when trying to use this method in the code above, the letter B is not found in the word Fred, therefore it returns -1 to indicate it was not found. If we then attempt to use that value as a reference to an item of an array, it will fail.

To help avoid these exceptions, it is usually good practice to perform a check before attempting to reference an item of a collection. For example...

int indexPointer = 2;
if (indexPointer >= 0 && indexPointer < myArray.length){
    // the index is within the bounds of the array
    Object myObject = myArray[indexPointer];
}

Canonical Question/Answers:

2854 questions
5
votes
8 answers

how to get city name using latitude and longitude in android

I am trying to get city in TextView using latitude and longitude. I am getting IndexOutOfBoundsException. AndroidGPSTrackingActivity.java import android.app.Activity; import android.location.Address; import android.location.Geocoder; import…
5
votes
2 answers

Set breakpoint in gdb on array out of bounds for gfortran program

I have a Fortran program compiled with gfortran with the -fcheck=bounds compiler option. This causes the code to report "array out of bounds" errors and subsequently exit. I would like to debug my program using gdb to find the cause of the error.…
Holger Schmitz
  • 361
  • 1
  • 6
5
votes
1 answer

C# ColorMatrix Index out of Bounds

I'm trying to run some slightly-modified code from an MSDN article as part of a school project. The goal is to use a colormatrix to recolor a bitmap in a picture box. Here's my code: float[][] colorMatrixElements = { new float[]…
Micah
  • 514
  • 3
  • 11
5
votes
1 answer

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

My problem is that I keep getting the error in the title during debugging. array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10 The following is the for loop. I entered values people = 10, payer = 4, rest = 6. int[] array =…
awefawe
  • 75
  • 1
  • 6
5
votes
1 answer

C# Strange Index Out Of Bounds exception

I have a strange problem with an Index Out of Bounds exception when debugging an addin I am developing for MS Outlook 2010. I have a class to make message processing and in the constructor for that class, I pass a MailItem. I then intend to run…
octagon
  • 51
  • 1
5
votes
3 answers

How can i check to see if a row has been selected?

I have a button click event on which i am getting a column value, if a Table row is selected. But if i don't select the row and click the button i get the error: java.lang.ArrayIndexOutOfBoundsException:-1 my question is how can i check to see if a…
user3747557
5
votes
3 answers

Index was outside the bounds of the array exception

Here is my code to get data from a flat file and insert into SQL Server. It is generating an exception (Index was outside the bounds of the array). string path = string.Concat(Server.MapPath("~/TempFiles/"), Fileupload1.FileName); …
user3368644
  • 79
  • 1
  • 1
  • 5
5
votes
4 answers

Printing Arrays/Reverse Array

So I have a question regarding printing arrays. These methods receive data from an array created from a file. The output should be 10 integers long in every row for as many integers are contained in the file. Let's say that the file contains {0, 1,…
a12b23c34
  • 65
  • 1
  • 6
5
votes
2 answers

String out of index with SQL developer

I got below message from sql developer when I try to access. I did just nothing special. my connection info like below Connection name : z*sch*** Username : root Password : it just numbers and characters(en) Connect Type : basic Role :…
Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121
5
votes
1 answer

Luke says my Lucene index directory is Invalid

I'm trying to learn about Lucene, and hope to use Luke to investigate it. I tried building an index with the IndexFiles demo in Lucene 4.3, then tried viewing the index with the latest version of Luke, and I'm getting the message: Invalid directory…
user3124028
  • 51
  • 1
  • 3
5
votes
1 answer

IndexOutofBounds when using Java's read bytes

I am using a RandomAccessFile in Java 6 but having some strange behavior when reading bytes. With the following code, where offset and data are appropriately initialized: int offset; byte data[]; randFile.readFully(data, offset, data.length); I get…
jaynp
  • 3,275
  • 4
  • 30
  • 43
5
votes
4 answers

Why can't I use "." as a delimiter in split() function?

I have to take an input file, and append a number at the end to its name to use as output file. To achieve this, I use the following code: String delimiter = "."; String[] splitInput = inputLocation.split(delimiter); String outputLocation =…
DT7
  • 1,615
  • 14
  • 26
5
votes
2 answers

Index out of range exception in 2D Array (C#)

char[,] map = new char[10, 20]; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; i < map.GetLength(1); j++) map[i, j] = '.'; } I just simply want to make all the elements of map[i,j] to be a point , but always when I try to…
5
votes
1 answer

java.lang.ArrayIndexOutOfBoundsException when getting image array

When I try to run the following code SampleRun.java public class SampleRun { public static void main(String[] args) { JlibFprint jlibfprint = new JlibFprint(); JlibFprint.fp_image_data fpimg = new JlibFprint.fp_image_data(); …
rachana
  • 3,344
  • 7
  • 30
  • 49
5
votes
2 answers

ExpandableListView getChildrenCount not called after data changed, leading to IndexOutOfBoundsException

In my Activity, I change the underlying data HashMap for my ExpandableListView, and then call ((BaseAdapter) myExpandableListView.getAdapter()).notifyDataSetChanged(); However, the app crashes after calling this, raising an…