• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.ArrayIndexOutOfBoundsException: 2 >= 0  RSS feed

 
rajnish patel
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am working on a project and getting this error " java.lang.ArrayIndexOutOfBoundsException: 2 >= 0"
I've problem related to this, I know that this is ArrayIndexOutOfBoundsException :P but
taking look at this sample example,



we get following error,
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

Here we don't see anything like, x<=y

and in my code, i amg etting 2>=0 error after ArrayIndexOutOfBoundsException
So my problem is that, when do we get such 2 >= 0 like errors ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That just means that you have a zero-size array, and you're trying to access element 2. Which doesn't exist.

So either the array hasn't been created correctly - look at the code that created it - or it's supposed to be allowed to have zero size but you've got elsewhere that incorrectly assumes the size is at least 2.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your array list contains 3 elements
Indices start at 0
So when you say
you are actually asking for the 4th element which does not exist, which is what the exception means
 
rajnish patel
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Your array list contains 3 elements
Indices start at 0
So when you say
you are actually asking for the 4th element which does not exist, which is what the exception means






First of all thank you for reply Sir.
and I knowwhat you said but my question is that why didn't I get error like x>y ??

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.IndexOutOfBoundsException is thrown by ArrayList when you access an element with an illegal index.

ArrayIndexOutOfBoundsException, with the "x >= y" message, is thrown when you access an array (not an ArrayList) with an illegal index.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!