How do I define the index within the array?

Why am I getting array index out of bounds exception?

  • say I have a method which among its arguments takes an int. I use that int in the method to index into an array which is an instance variable. in the first call of that method, intarg is zero. Why am I getting an exception when the array is guaranteed to have a 0th element? private void someMethod(somearg, somearg, intarg) { .................................. ........................... ....................... myArray[intarg] += someint; }

  • Answer:

    As mentioned in the comment, looks to be a problem in the initialization code or hang on.. it can't be initialization error since then we would have had NullPointer. Do make sure the intarg is received as 0 and not any other number. Also make sure the intarg is not modified before reaching that part of the code in the method. (Parameters are not readonly and can be modified.)

Chandra Mohan at Quora Visit the source

Was this solution helpful to you?

Other answers

Check the following : 1. Is the int parameter arg being reinitialized somewhere in the method ? if not, may be consider making it final so as to avoid a red herring 2. An array is not guaranteed to have a 0th element. Its perfectly legal to declare an array of size 0. In that case even arr[0] will give IndexOutofBoundsException For example, the following code will throw java.lang.ArrayIndexOutOfBoundsException : int[] arr = new int[0]; System.out.println(arr[0]); 3. Consider enabling assertions and adding an assertion checking if the length of myArray is >= arg

Kaustubh Saha

Array index out of bounds exception occurs, for example, when you have 3 elements in the array and you are trying access element 4th element. This usually occurs when we forget that the indexing starts from 0. Remeber that you can start the loop and end at any number when you want execute a specific set of lines for certain number of times. But when you are looping to access elements with the indexing variable you should be careful about the index.

Amar Chaitanya

I thought you were not initializing your array properly. As you have not got NullPointerException means you did initializing. With out seeing your code I can not describe where is actual problem but the code you are showing is absolutely correct, you can do this with instance variable.I will recommend you to please access your array like this.[code]public class MyList<E> { private int size = 0; private static final int DEFAULT_CAPACITY = 10; private Object elements[]; public MyList() { elements = new Object[DEFAULT_CAPACITY]; }public void add(E e) { if (size == elements.length) { ensureCapa(); } elements[size++] = e; }private void ensureCapa() { int newSize = elements.length * 2; elements = Arrays.copyOf(elements, newSize); }@SuppressWarnings("unchecked") public E get(int i) { if (i>= size || i <0) { throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i); } return (E) elements[i]; } }[/code]Happy Coding

Shiv Pratap Singh Parmar

Possibilities of exception in your case array you haven't initialized array like type ary[]=new type[length] or type ary ={             type_value,type_value};2. Array is empty and you are trying to fetch the value3. the index value doesn't exit in that particular array for that you can check the length of array first . like in your case length will be one 4. Maybe you are using the wrong array

Pavneet Twinkle

Hello,I request you to go through this link,http://java.meritcampus.com/t/28/Array---overview to get an overview on Arrays.We also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: http://java.meritcampus.com/ to know more. 

Merit Campus

It is legal to have an array with size 'zero' in Java. Example : int[ ] a = new int[0]; System.out.println(a[0]); // Runtime Exception : ArrayIndexOutOfBoundsException: 0 But you will get runtime exception AIOBE. Know more - AlgoValley

Manvendra Priyadarshi Maan

this error simply means u are trying to access an element that is not in the array. check your logic and dry run your program before execution. you will know what is wrong

Rachit Srivastava

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.