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

SweepGradient ArrayIndexOutOfBoundsException

I create a SweepGradient like this int[] colors = { Color.RED, Color.BLUE }; // float[] positions = {0,1}; => this will work without error float[] positions = { 0 , 280f/360 }; SweepGradient gradient = new SweepGradient(width / 2, height / 2,…
Linh
  • 57,942
  • 23
  • 262
  • 279
0
votes
1 answer

Array index out of bound exception while downloading elastic search index

I am trying to download complete elastic search index using: curl -o output_filename -m 600 -GET 'http://ip/index/_search?q=*&size=7000000'. But its giving error: {"error":"ArrayIndexOutOfBoundsException[-131072]","status":500} How can I download…
Mohit Jain
  • 357
  • 2
  • 7
  • 18
0
votes
1 answer

Index Out of Bounds Exception handling

Trying to handle an exception. Using an If statement at the recommendation of cricket_007 who has been incredibly helpful! However, still struggling through it. Basically, answering the first string question in my gui, and then incrementing the…
Ted
  • 1
  • 1
0
votes
2 answers

Android - Array index Out Of bounds but why?

One of my users is experiencing a problem with my app. I have received he stack trace but it's not really pointing me to the problem. All I knoe is that some index is out of bounds, but which index? The trace does not lead me to my code... I…
Bavilo
  • 389
  • 1
  • 6
  • 19
0
votes
1 answer

Why do I get IndexOutOfBoundsException at index = -2147483648 (= Integer.MIN_VALUE)?

The following code runs until it throws an IndexOutOfBoundExceptionstating h is -2147483648. At the beginning h=0and it only gets incremented, never decremented. How could it become negative? float[] powerArray = new float[8760]; int h = 0; for…
Stefan
  • 10,010
  • 7
  • 61
  • 117
0
votes
1 answer

Receiving error(IndexOutOfBoundsException) when its higher number of objects in the array list

I know this is a common issue and I have already read some of the posts but could not figure out the root cause for the error yet. I have devices which get parameters from a json file and the code generate topologies for that devices. I have a list…
Dudu OK
  • 3
  • 5
0
votes
1 answer

java: Printing sorted arrayList is giving me IndexOutofBoundsException

This is my Scheduler Class that sorts the arraylist. I made a custom compareTo method that returns an int value of 1, -1 or 0. private ArrayList events = new ArrayList(); public Scheduler(ArrayList events){ for (int…
Jay-z
  • 1
  • 2
0
votes
0 answers

Struggling to design a sliding 3x3 puzzle with methods to swap tiles

I am trying to design a simple 3x3 sliding puzzle game. The only tile you can move is a blank tile and this can be swapped with any of its neighbouring tiles. There is currently no final goal for the game but I have had trouble implementing the…
0
votes
3 answers

Why am I getting an java.lang.ArrayIndexOutOfBoundsException exception?

I am writing the following code for a program that returns a boolean of whether or not three consecutive numbers in an array of ints add up to 7. I am getting the following exception instead of the output that I want:…
Jonathan Math
  • 101
  • 2
  • 7
0
votes
1 answer

Winform How to increment items in a list

I'm new to programming in general and starting off with C# I've been told to add items to a list box without use of any arrays only by converting and parsing. However I cannot seem to get my code to work, im trying to increase the number in the…
0
votes
1 answer

XMLWorkerHelper Exception java.lang.IndexOutOfBoundsException: Index: 11, Size: 11

I am trying to execute the below code. But getting this exception: java.lang.IndexOutOfBoundsException: Index: 11, Size: 11 Does anyone has any idea what is causing this exception and how to resolve it? private ByteArrayInputStream…
Ashwini
  • 125
  • 3
  • 12
0
votes
1 answer

Updating cursor after returning from intent to mainactivity

So i am having this listview in my MainActivity, which is updated every time the main activity is started or resumed. The code for that is @Override public void onResume() { super.onResume(); Cursor array_list_patients =…
0
votes
4 answers

Error handling when reading from txt file

I have my program which reads from a txt file, then turns txt information into a car object and adds them to an arraylist. try { String filePath = "car.txt"; File f = new File(filePath); Scanner sc = new Scanner(f); List
Buupu
  • 67
  • 1
  • 8
0
votes
1 answer

Check if index is OutOfBounds in a 2D array

So I'm building a sort of "Game of Life" application in java and are having some trouble detecting when the index is out of bounds.The code below is a modified version from another qustion here from SO. 2D…
0
votes
2 answers

index out of bounds exception strings 8

I'm trying to set up a compression algorithm that places a number before a char in a string when that char can be seen in succession. EX: for the string "balloonnnnns" it would be compressed to "ba2l2o5n" but I am receiving an index out of bounds…
ts_928
  • 15
  • 1
1 2 3
99
100