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

Strange Jsoup OutOfBoundsException when fetching html

When using 12 in iteration (inside for) it output correctly, when assigning 13 to iterator (inside for) throws IndexOutOfBoundsException on line imageUrl[i]=images.select("img").get(i).absUrl("data-original");`. Note: class lazy in…
Farid
  • 2,317
  • 1
  • 20
  • 34
3
votes
1 answer

Polynomial Division

I am making a program to calculate operations over polynomials and I already finished other operation (+ - *) but stucked with the division, I am getting this error always as it shown in the code static int[] DividePolnomials(int[] pol1, int[]…
Omar
  • 98
  • 2
  • 12
3
votes
1 answer

Receiving "javax.net.ssl.SSLException: java.lang.ArrayIndexOutOfBoundsException" while connecting to "https:" site

I am trying to connect to a "https://" site which supports "TLSv1" protocol and the following cipher suites, TLS_DHE_RSA_WITH_AES_256_CBC_SHA (0x39) DH 4096 bits TLS_DHE_RSA_WITH_AES_128_CBC_SHA (0x33) DH 4096 bits using Apache's httpclient…
3
votes
3 answers

What can I return nothing if a specific condition is not true

I'm writing a Matrix class and I wrote a getNumber method which returns the number in a specific slot in the matrix. If the specific slot does not exists, what can I do? I got stuck here: public int getNumber(int row, int column) { if (row <…
Haggra
  • 3,539
  • 2
  • 20
  • 28
3
votes
1 answer

Out of Bounds error when Implementing an insertion sort

It seems that VB.NET evaluates both sides of the Boolean condition While j >= 1 And Card(j - 1) > NextCard, which will of course lead to an out of bounds error. Other languages I have programmed in would have evaluated the left condition first and…
3
votes
1 answer

"Index Out Of Range" While refreshing table - Swift

I have two buttons in the view controller and a table view below them. The table view is refreshed when one of the two buttons is pressed with different filter parameters. Actually when the table view is loaded and then the other button is clicked…
saner
  • 821
  • 2
  • 10
  • 32
3
votes
1 answer

Android Realm Java 0.82.1 ArrayIndexOutOfBoundsException

I'm getting this error when trying to update an objet from realm (0.82.1): rowIndex > available rows: 0 > 0 Realm realm = null; try{ realm = Realm. getInstance ( context ); RealmResults results = realm.allObjects( Track.class ) …
mrpep
  • 131
  • 3
  • 12
3
votes
2 answers

Why am I getting Mockito misplaced argument matcher detected here with anyInt()?

Mockito.when(metadataDao.getStuff(Mockito.anyInt()).get(Mockito.anyInt())) .thenReturn(returnedVariable); I'm getting the following error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at…
Grégoire Borel
  • 1,880
  • 3
  • 33
  • 55
3
votes
1 answer

ArrayIndexOutOfBoundsException while opening stream from SSL-secured WSDL

I get a confusing ArrayIndexOutOfBoundsException at every SSL-secured Webservice call and I have no idea what's the reason. The exception comes without any relevant change on the code over night and I am baffled. Can anyone give me a hint how I can…
KreMat
  • 41
  • 7
3
votes
1 answer

I get IndexError while still in the range

I am trying to read the rows of a csv file. My file looks like this Col 1, Col 2, Col3 row11, row12, row13 row21, row22, row23 row31, row32, row33 ... I use the following command to read the rows with open('~/data.csv') as f: r =…
K1.
  • 275
  • 1
  • 2
  • 10
3
votes
3 answers

String index out of range: -1 error with loops

I checked old topics with this problem and couldn't fix it. This method is meant to compare two strings, and if every letter/character of the first string is found in the second string (not necessarily vice versa), then the method should return…
jessca
  • 33
  • 5
3
votes
3 answers

ArrayIndexOutOfBoundsException error in parsing JSON data

I am getting ArrayIndexOutOfBoundsException at id[i] = c.getString(TAG_ID); Here's the code: for (int i = 0; i < 25; i++) { JSONObject c = contacts.getJSONObject(i); id[i] = c.getString(TAG_ID); } I have checked that JSON file contains 25…
Harshil Pansare
  • 1,117
  • 1
  • 13
  • 37
3
votes
2 answers

notifyDataSetChanged() causes IndexOutOfBoundsException

Here's a shortened version of my code. public class MainActivity extends ActionBarActivity { private ArrayList entry = new ArrayList(); private String[] entryString = new String[11]; private ArrayAdapter aa; private ListView…
3
votes
3 answers

Netty: Weird IndexOutOfBoundsException: readerIndex + length exceeds writerIndex

I am currently sending different packets through netty and I'm very often getting exceptions like these when receiving them: java.lang.IndexOutOfBoundsException: readerIndex(39) + length(32) exceeds writerIndex(64): UnpooledUnsafeDirectByteBuf(ridx:…
Kombustor
  • 63
  • 1
  • 1
  • 5
3
votes
1 answer

how to get the row item position number in listview adapter class

I have followed other questions and have done what they have mentioned but didn't get the result. my getTag() gives me ArrayIndexOutofBound error. Adapter Class private final Home context; private LayoutInflater inflater; private int…
Fay007
  • 2,707
  • 3
  • 28
  • 58