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

How do i fix this runtime error? Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

import java.util.Scanner; //importing scanner to get user input public class ArrayHelper { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] hello = new int[10]; for(int i = 0; i…
user7531247
0
votes
1 answer

Scala: IndexOutOfBoundsException: -1

I went step by step looking at this function and it seems to me that it should avoid calling sortedCoins(0-1) by using only sortedCoins(0) and terminating when y == -1, but somehow it doesn't. Why? def countChange(amount: Int, coins: List[Int]): Int…
Dominik
  • 121
  • 1
  • 2
  • 10
0
votes
0 answers

Index was outside the bounds of the array in SQL Server

I installed SQL Server 2008 R2 on my PC and while I am running queries it throws the error: Index was outside the bounds of the array. (Microsoft.SqlServer.Smo) Before querying the table when I click on table name I see above message. How can I…
0
votes
1 answer

Why is the index out of bounds of the array when I'm resizing the array?

Here's a small piece of the program I am working on. I am trying to manually resize the array by creating another array, copying all of the items from the first array into the second and then having the first array refer to the second. RoundInfo[]…
0
votes
1 answer

Cursor index out of bound error

I'm experimenting with the database and here I've not used the helper class but added the db functions within the activity. I've got a index out of bound error while trying to select a column from the obtained row. Specifically…
Nobody
  • 397
  • 1
  • 5
  • 25
0
votes
1 answer

Remove / delete selected cells from UICollectionView causes index out of bounds [sometimes]

I have a comments array declared as: var comments: [String] which I populate it with some Strings and I also have a UICollectionView within which I present the comments. My code is the following when I try to delete the selected cells from the…
TheoK
  • 3,601
  • 5
  • 27
  • 37
0
votes
1 answer

Sorting of array by bubble sort

I am trying to sort the array using bubble sort algo in Java. But when I run the code an ArrayIndexOutofBoundException occurs. Here is my code package bubblesort; public class BubbleSort { public int[] sort(int [] arr){ int temp=0; …
0
votes
0 answers

JComboBox Error on removeAllItems()

I'm trying to call ports.removeAllItems(); with ports being a JComboBox, I am getting this error Exception in thread "Thread-3" java.lang.IndexOutOfBoundsException: bitIndex < 0: -1 at java.util.BitSet.get(BitSet.java:623) at…
0
votes
2 answers

How can I sort a string array in ascending order, by number of vowels in each string?

import java.util.*; class VowelAsc { public static void main(String args[]) { int count=0; Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String [] s=new String[n]; …
mahesh
  • 1
  • 7
0
votes
1 answer

ArrayList Error, App Crashes

I have the following problem. I have a two fragment application with a static arraylist which is used in both fragments. The occurance of the error can be described like this: Add entries to arraylist (FragmentA) Hit button, display arraylist in…
0
votes
0 answers

Random "Index was outside the bounds of the array"

I have this piece of code in my program that sometimes, like once a week, keeps exiting with this excpetion, but I really can't understend which part of the code is wrong. Can someone analyze it and help me figure this out? I have to do a "power…
Nedo2490
  • 9
  • 1
  • 7
0
votes
2 answers

Why no out of bounds error on for loop (Java)

I'm doing a problem on codingbat.com and am confused with why this solution to the problem does not give an index out of bounds error. Wouldn't the first for loop search for an index that is beyond the length of the passed array? Here's the…
Ethan
  • 223
  • 1
  • 2
  • 4
0
votes
1 answer

select all option for listview with checkbox

I'm fetching phone contacts using listview with checkboxes. I used the instrucstions given in this link. The code is working perfectly if I want to select some of the contacts. Now I want to add a "Select All" checkbox in the activity //…
Somnath Pal
  • 137
  • 1
  • 4
  • 10
0
votes
0 answers

Why does my array get out of bounds, and how should I adjust this?

I went over this code over and over, but somehow I get ''array out of bounds''. I started the for loop at one, because I want to have Car 1 and starting at Car 0 doesn't make sense. But somehow my array get out of bounds and the only solution I had…
Tony Stark
  • 45
  • 1
  • 8
0
votes
3 answers

ArrayIndexOutOfBoundsException in Hangman game

I'm trying to create a Hangman game but I don't know how to properly initialize the wordDisplay array, it gives an error this array is supposed to print out underscores for letters of the word to guess and then the underscores get replaced by…
coldman1
  • 23
  • 8