Add to Google Reader or Homepage

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException


ArrayIndexOutOfBoundsException:


The ArrayIndexOutOfBounds Exception is common in the java program that uses array for storing the values.There are many exceptions associated with the arrays like ArrayStorageException and ArrayIndexOutOfBounds Exception.Let us discuss about the ArrayIndexOutOf Bounds Exception.

What is meant by ArrayIndexOutOfBoundsException?

The ArrayIndexOutOf Bounds Exception  message gives us some knowledge about this exception.This exception message indicates that a particular statement in the program tries to access the value in the index(Position number of the particular value in the array) which is either greater than or negative or equal to the size of the index.

For eg) Let String a[]={"a","b","c"};

Here a[0]=a;
         a[1]=b;
         a[2]=c;

Here 1,2,3.. represents the index value.

Briefly said ,it shows us that we are trying to access the index position which is either greater or negative to the size of the array.

When will be this exception is thrown?

As i have said before ,the only reason for this exception to be thrown is: using the index(position) greater than or negative to the size of the array.For eg.If the size of the array is 4 and we are trying to access the value at index 5 will cause this exception to be thrown.

How to Fix this Exception?

In many occasions you might think of increasing the size of the array to mitigate this exception.But this is not the desired solution.If your programming logic is not correct then even if you increase the size of the array this exception will be thrown.So try to correct the statement that causes this exception.So while dealing with arrays ensure that your statement tries to access the value only within in the size of the array.

Example situation of this exception:

public class Desc
{
public static void main(String args[])
{
int t;
int a[]={2,7,4,5,6};
for(int j=0;j<5;j++)
{
for(int i=j+1;i<=5;i++)
{
if(a[j]<a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int h=0;h<5;h++)
System.out.println(a[h]);
}
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
        at Desc.main(Desc.java:11)

In the above code,line no 11.for(int i=j+1;i<=5;i++) contains a statement which tries to access the index equal to the size of the array.So this statement causes an ArrayIndexOutOfBoundsException

2 comments:

aravind said...

I get error
says "Exception in thread main java.lang.SQLException [Microsoft][ODBC Driver Manager] data source name is not found and no
default driver specified".

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top