• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected ArrayIndexOutOf BoundsException.

 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I following code compiled fine but at runtime it threw ArrayIndexOutOf Bounds Exception . I am unable to figure out what went wrong.Here's the code

Thanks in advance
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What args did you pass?
Can you post the stack trace?
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't pass in any arguments then you're trying to access element -1 of the array, which will cause an exception.

Also, Integer.valueOf is unnecessary unless you're using some ancient version of Java (see autoboxing).

edit: never mind, I too was confusing this Integer.valueOf(String) method with Intger.valueOf(int) - the latter of which is a waste of space since the compiler in Java 5 or later does it for you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is probably worth using valueOf after all, because it permits caching of small values.
 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah , i got u. Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although this valueOf method converts a String into an Integer, I think that parseInt would be better because it bypasses the Integer object and immediately turns the String into an int.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was mistaking it for the Integer.valueOf(int) method.

Agree that for turning a String to an int parseInt() is a lot better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic