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
4
votes
0 answers

Array Index out of bound when Scrolling list view fast while updating data from server

I have a list view which is updated with items coming from server. On this list view I have load more functionality and pull down to refresh both. Everything is working fine but problem is when I pull down to refresh and and scrolls the listview…
Android
  • 1,085
  • 4
  • 13
  • 28
4
votes
2 answers

Index out of bounds in a Spinner

There are 4 Spinnners: resiko,untung1,untung2,untung3 The user picks resiko first to proceed and the app will show which Spinner will be visible (untung1/untung2/untung3) Trying to make dynamic data Spinner, when you click an item inside the first…
4
votes
7 answers

Index was outside the bounds of array at trimmed text

I have created a Windows Form App which is connected to a database and users enter specific values in it. The problem I'm facing right now is that I created a search, which works like charm. But, users will always search by Name and Surname, and I…
Ezekiel
  • 333
  • 4
  • 9
  • 27
4
votes
4 answers

Parse CSV file in java, and delaing with empty values

I am parsing a CSV file into my program, spliting the values at the , element, and it's working fine, except for when I have lines that have missing values. The parser is working like this: import java.io.BufferedReader; import…
Rob Sked
  • 277
  • 1
  • 5
  • 15
4
votes
1 answer

OutOfBoundsException with ALS - Flink MLlib

I'm doing a recommandation system for movies, using the MovieLens datasets available here : http://grouplens.org/datasets/movielens/ To compute this recommandation system, I use the ML library of Flink in scala, and particulalrly the ALS algorithm…
4
votes
2 answers

Java: importing from text file to ArrayList - out of bounds errors

I am a student learning java and I am having a tough time trying to figure out why i am getting an out of bounds error. Any input on how to code the openFileList(); method would be extremely helpful. The precise error is : …
4
votes
2 answers

Is array bounds checking eliminated in release builds?

I've read that array-bounds checking can slow down hot spots in programs because with each access of the array, an additional instruction must be executed to ensure it is within the bounds of the array. As a result, sometimes people recommend…
4
votes
6 answers

Substring strange Bounds?

So, I am trying to figure out why String.substring(int startIndex) allows a start index that is out of bounds and does not throw OOB exception? Here is the code I was testing this with : public class testSub { public static void main(String[] args)…
Kameron
  • 117
  • 1
  • 11
4
votes
1 answer

ArrayIndexOutOfBoundsException error although I don't use arrays

I'm new with java. Although my code is too simple, I'm encountering this error irregularly. I don't use any arrays, I only have 4 Jbuttons, one Jtextpane without performing any event!!! Code: private JToolBar bar = new…
E.maghout
  • 43
  • 6
4
votes
5 answers

Understand Arraylist IndexOutOfBoundsException in Android

I get a lot of IndexOutOfBoundsException from the any Arraylist I use. Most of the times it works fine but sometimes I get this annoying error on Arraylists i use on my project. The main cause is always…
lil'ms
  • 452
  • 3
  • 5
  • 17
4
votes
2 answers

java.lang.IndexOutOfBoundsException: Index 7, Size:7

I am creating a program that finds the solutions to the eight queens problem by breadth search. My code so far: import java.util.*; import java.lang.*; import java.io.*; public class EightQueens { public static void main(String[ ] args) { …
user3066398
4
votes
2 answers

Would this be an example of short circuiting?

If I ask a user to input an int, and need to check if it is within the range of indices of an array before checking the array at that index to see if it's not null, would that be an example of "short circuiting"? Because if the array size is only 5…
user3059511
4
votes
1 answer

Illegal Escape Character "\("?

I need to escape the ( character without the output becoming anything other than.. a b Any help greatly appreciated! Arbitrary Input: "a"+"\n" +"("+"b" Desired Output: a b //Here are the attempted work-arounds that failed 40 String test =…
user3025996
  • 47
  • 1
  • 1
  • 4
4
votes
3 answers

Why am I getting this exception

I have a System.Data.DataSet and 1 single table in it. The table has many columns. In a certain event handler, I am setting a decimal value for one of the fields, in a data row which is already existing (at the time of setting). In a very rare…
ViV
  • 1,998
  • 8
  • 27
  • 54
4
votes
2 answers

Eclipse startup exception (java.lang.ArrayIndexOutOfBoundsException in ChooseWorkspaceData.writePersistedData)

Just installed eclipse 3.8, it gives me a exception everytime i select a workspace. I tried clearing the metadata directory but still no use. I am new to eclipse and dont understand much of its configurations. !SESSION 2013-09-26 11:57:16.290…
Kratos
  • 41
  • 1
  • 3