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

How to solve this java.lang.ArrayIndexOutOfBoundsException: 7

I was trying to make a minesweeper program for homework, but I got a problem with making and positioning mines that hasn't solved for 3 hours. My code is for(int i = 0; i < bomb; i++) { int x = randomGenerator.nextInt(m); int y =…
0
votes
6 answers

2D ArrayList Index Out of Bounds

I was implementing 2D Array for my APP. But unfortunately I had this error. FATAL EXCEPTION: main Process: com.application.recast, PID: 18534 java.lang.RuntimeException: Unable to start activity…
0
votes
1 answer

IndexOutOfBoundsException in TextWatcher

In my app I want to track the input in an EditText. Each time the user modifies a character in the EditText I want to track: If a character was added/deleted The index at/from which the character was added/deleted and what character was…
0
votes
1 answer

Snippet of 1 Array to Check for Recurring Elements

I was working on using only 1 array and a nested for loop to check for repeated elements and turn them to 0. I have an IndexBound problem and can't quite tell what's wrong. Any help? int data[] = new int[20]; for(int i = 0; i < data.length;…
Asker123
  • 350
  • 1
  • 3
  • 15
0
votes
1 answer

Recyclerview arraylist throws an outofbounds exception in android

Im having an issue with my recycler view or more accurately with a secondary arraylist used to set a value in each viewholder. I have a list of items sent to my adapter and in its constructor I create an arraylist the size of this list. An arraylist…
0
votes
2 answers

Java: newConstraints isn't working

I am trying to create buttons at random places in frame in x and y axises with different gridwidth and gridheight but the thing is,i am not able to run it cos i am getting errors :/ here is my code.. What am i doing wrong? public class calculator…
the feels
  • 107
  • 1
  • 7
0
votes
0 answers

I get an ArrayIndexOutOfBoundsException while using the BufferedReader and the .split() method

So basically, I'm trying to keep taking input from the user and printing it line by line, until he's done, then I'll print done. Problem is, the code works just fine until I press double Enter/Return, it's supposed to print DONE however it returns…
0
votes
1 answer

Unable to get List from response.body() from GET Retrofit Android

I am trying to get List from GET request with retrofit. But whenever I try to access the value from the list value from the response.body() (which is successful to retrieve the data from database),the error is always index out of bound, which the…
Kenneth951Lo
  • 7
  • 1
  • 5
0
votes
2 answers

I want to check if index of ArrayList is declared, without using get

I try to get an element from an Arraylist using an index, and check if it is empty or not. I tried: if(!ArrayList.get(index)) { // do something } And it gets Index out of Bounds exception. Of course I can use try-catch, but I'd like to know if…
0
votes
0 answers

Datagridview Index was out of range

I'm doing a simple datagridview update function I have 2 forms. 1 for datagridview and another for the textboxes for update purpose. An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional…
0
votes
2 answers

six error messages "main" java.lang.ArrayIndexOutOfBoundsException: 2

When I tested the below code it was running well with one dataset but when i test it with another dataset , it gave me this thread Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at…
anas
  • 3
  • 4
0
votes
2 answers

String array always throws ArrayIndexOutOfBoundsException

I'm trying to fill a string array with values through user input. I'll be doing more with this class later, but just want to see the array filled and print it out. I'm using a while loop to fill to a specific value, waiting for the user to enter 0…
Jerevand
  • 9
  • 3
0
votes
3 answers

how to get an instance of an arraylist which is inside another arraylist

I have an arraylist inside an arraylist (nested arraylist) as below ArrayList> indexOfJSONObject = new ArrayList>(); Now I need to get an instance of the arraylist that exists in a given index of the…
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
0
votes
2 answers

How to fix out of bounds index error?

FIXED: I removed my while loop and added if (num.contains(count) == true) { freq = Collections.frequency(num, count); for (int k = 0; k < freq; k++) { sb.append(temp); } …
Yawn
  • 173
  • 3
  • 20
0
votes
1 answer

ArrayIndexOutOfBoundsException xmlserializer

I have a code that creates a kml file using XMLserializer. It works fine creating the different polygons, but I'm having an error with closing the xml that I really don't understand. 02-09 10:03:02.919 24712-26301/com.example.accgps W/System.err:…