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
7
votes
2 answers

Why the following code does not throw IndexOutOfBoundsException, and print out 9 9 6?

I am new to java. I had a doubt. class ArrTest{ public static void main(String args[]) { int i = 0; int[] a = {3,6}; a[i] = i = 9; System.out.println(i + " " + a[0] + " " + a[1]); // 9 9 6 } }
7
votes
0 answers

Mysterious indexOutOfBoundsException involving a measureLimit in Android

My code works perfectly 99% of the time, but if I mash the keyboard long enough, I can sometimes get it to throw an IndexOutOfBounds exception. I can't tell you exactly how to reproduce it, because I can't reproduce it myself without literal minutes…
7
votes
4 answers

Getting list of images from a folder in android kotlin

I'm trying to get list of images from a folder using this function var gpath:String = Environment.getExternalStorageDirectory().absolutePath var spath = "testfolder" var fullpath = File(gpath + File.separator + spath) var list =…
7
votes
7 answers

Getting ArrayIndex out of Bounds Exception when adding new items to list while scrolling

I have a ListView in which I load data from SQLite by setting a limit of 3 data from SQLite when scrolling the list I have used AsyncTask to load another 3 data from the database, but when the new data is loaded it shows me an error:…
7
votes
3 answers

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - Database Reading - Android

I created a method that reads data from a database and puts it in a String array. Android Studio doesn't give syntax errors but when i launch my app the log says: 03-19 16:31:20.938 2518-2518/com.mms.dailypill E/AndroidRuntime﹕ FATAL EXCEPTION:…
7
votes
1 answer

IndexOutOfBoundsException in my Android Listview adapter

I keep getting this IndexOutOfBoundsException, but can't seem to figure out what's causing it. My listview has an adapter with a list of objects, and the objects are removed based on a timestamp. The removal is done inside the getView method. Once…
kenneho
  • 401
  • 1
  • 7
  • 24
7
votes
3 answers

Android ArrayIndexOutOfBoundsException and AbsListViewRecycleBin.addScrapView

I have an ArrayIndexOutOfBoundsException that randomly comes up. It seems to be happening during my notifyDataSetChanged(); Since the error is so random it makes it difficult to pin point exactly where it is happening. Has anyone had similar issues…
K3NN3TH
  • 1,458
  • 2
  • 19
  • 31
7
votes
2 answers

java.lang.ArrayIndexOutOfBoundsException: 0

I am learning java using a book. There is this exercise that I can't get to work properly. It adds two doubles using the java class Double. When I try to run this code in Eclipse it gives me the error in the title. public static void main(String[]…
pg2014
  • 89
  • 1
  • 1
  • 3
7
votes
2 answers

IndexOutOfBoundsException at android.graphics.Paint.getTextRunAdvances(Paint.java:1774)

In my application one of user sometimes get error and application crashing. User send me log: java.lang.IndexOutOfBoundsException at android.graphics.Paint.getTextRunAdvances(Paint.java:1774) at…
BArtWell
  • 4,176
  • 10
  • 63
  • 106
7
votes
3 answers

JVM bug? Cached Object field value cause ArrayIndexOutOfBoundsException

This is kind of strange, but code speaks more then words, so look at the test to see what I'm doing. In my current setup (Java 7 update 21 on Windows 64 bit) this test fails with ArrayIndexOutOfBoundsException, but replacing the test method code…
Sebastien Diot
  • 7,183
  • 6
  • 43
  • 85
6
votes
1 answer

MessageDigest ArrayIndexOutOfBoundsException

I use MessageDigest to calculate the md5 signature in my project, but during the performance test it throws an ArrayIndexOutOfBoundsException. I have found a few posts that suggest this is because MessageDigest is a singleton and not thread safe.…
Vince.Wu
  • 870
  • 1
  • 10
  • 17
6
votes
3 answers

Images in SimpleCursorAdapter

I'm trying to use a SimpleCursorAdapter with a ViewBinder to get an image from the database and put it into my ListView item view. Here is my code: private void setUpViews() { mNewsView = (ListView) findViewById(R.id.news_list); Cursor…
6
votes
3 answers

Spark throwing ArrayIndexOutOfBoundsException when parallelizing list

I've tried to create a Spark implementation of QuickSort to test against a serial implementation. I've got the serial implementation working, but the parallel implementation throws an ArrayIndexOutOfBoundsException when attempting to parallelize a…
6
votes
5 answers

Java - How to create a deck of cards (with a focus on static arrays)? - AP Computer Science Project

this is my first question ever. Just to clarify, I did check to see if there were any questions that could have helped me before asking this. Apologies in advance if I do anything incorrectly, I'm new. Anyways, for my AP CS class I must make a deck…
user9125722
6
votes
1 answer

In which case will C++ do array bounds checking at compile time?

Inspired by the idea of "C++ HTML template engine that uses compile time HTML parsing", I am trying to write a sample class to check whether the first char in a string is a. int dummy[0]; class Test { public: constexpr Test(const char…
camino
  • 10,085
  • 20
  • 64
  • 115