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
1 answer

I'm getting the error below code in Selenium

public class AddEmployee2 { public static void main(String[] args) throws Exception { String dateTime ="26/02/1989"; WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); …
0
votes
2 answers

File to Array to JTable (Index out of Bounds)

Brief description I am currently writing a massive (For my skill level) java program with my first UI. My goal is to make the program use the UI to save career information to a text file and allow it to read that file into a JTable, these two…
0
votes
0 answers

Why is my array out of bounds

I am attempting to make a program that allows two player checkers. The teacher said it must use a 2d array but when it runs it says the array is out of bounds. int[][] pieceStatus[3][7]; it is a four by eight because half of the squares on the…
Hobson
  • 11
  • 2
0
votes
2 answers

System.arraycopy does not throw ArrayIndexOutOfBoundsException

I don't understand exactly how System.arraycopy works. Have simple example: String[] arr = {"a"}; String[] copyArr = new String[10]; System.arraycopy(arr, 0, copyArr, 0, 1); System.out.println(Arrays.toString(copy)); I understand it like "copy 1…
swch
  • 1,432
  • 4
  • 21
  • 37
0
votes
0 answers

When trying to remove from "list" I am getting an ArrayOutOfBoundsException index=-1. Android

I am trying to make an array of checkboxes in a Checkbox container. If the "other" checkbox is selected, then an edittext pops up asking for specification. I also need to get the value of the checkboxes, which I am able to do by getting the String…
0
votes
1 answer

Getting error when selenium script run in background and executing switching from one tab to another

Exact error message : Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:653) code snippet: ArrayList tabs = new…
0
votes
1 answer

Getting ArrayIndexOutOfBoundException during left-shift operation on Array

This is the code which I tried to perform Array Left shift operation which works fine for a few inputs but as i am increasing the shift amount it gives an ArrayOutOfBoundException exception. import java.util.Scanner; public class ArrayShift…
0
votes
2 answers

Android - Errors in code that should be working

I have some code here for a calculator that I'm pretty sure should be working (I remember it working in the past), but it's acting up. I've checked all the arrayList.get() to make sure they're pointing to the right index, and there are the correct…
rst-2cv
  • 1,130
  • 1
  • 15
  • 31
0
votes
1 answer

Have an index error I don't know how to correct

I found this code elsewhere on the site, but for some reason I keep getting the same error message: products[row[0]] = [row[1], row[2], row[3]] IndexError: list index out of range. I am unsure how to correct this, any help is appreciated, thanks.…
0
votes
2 answers

Python - Array goes out of bounds

Good day everyone, today I struggled with my code for more than an hour, just to find out that Python did not give an out of bound error. This is my summarised code: array = [] n = 0 while n < 5: array.append(1) n = n + 1 print array for i…
Matthias
  • 23
  • 1
  • 4
0
votes
2 answers

Why do I keep receiving a java.lang.StringIndexOutOfBoundsException

I am trying to write a program that takes a string and removes all instances of another string from it. For example: ("Remove them all!", "em") would print "Rove th all!". However, when I do run this, it give me…
0
votes
2 answers

Clisp implementation of quickperm algorithm

I'm working on an iterative solution to the n queens problem. I've decided to represent the state space as an array of 0's and 1's, such that a 1 represents the presence of a queen. The plan is to generate all permutations of the array and then…
FutureShocked
  • 779
  • 1
  • 10
  • 26
0
votes
1 answer

2D ArrayOutOfBounds exception

I'm writing a program that lets the user input a state, and it will echo the state entered, along with the state bird and flower.I'm getting an ArrayOutOfBounds exception, and I don't know how this is occuring. This happens at the printArray…
Matt
  • 73
  • 11
0
votes
2 answers

Android RecyclerView Adapter Position is -1

I am making a todo list app where if I click on an item in a RecyclerView a Dialog will pop up that lets you edit the name of that task. On some tasks when I accept the changes the app crashes and this stack trace pops up. 10-02 15:10:34.801…
Jared
  • 2,029
  • 5
  • 20
  • 39
0
votes
1 answer

OutofBoundException when sending an array to a method

public class I5Exc1a { public static int[] reverse(int[] array) { int[] local = array; int i = local.length; int j = 0; int[] arrayR = new int[i]; for (;i>-1; i--) { arrayR[j] = array[i]; j++; } …
1 2 3
99
100