| Author |
Unexpected ArrayIndexOutOf BoundsException.
|
Catherine austin
Ranch Hand
Joined: Jul 20, 2011
Posts: 43
|
|
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
Joined: Jul 26, 2007
Posts: 8439
|
|
What args did you pass?
Can you post the stack trace?
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Luigi Plinge
Ranch Hand
Joined: Jan 06, 2011
Posts: 441
|
|
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
Joined: Oct 13, 2005
Posts: 32694
|
|
|
It is probably worth using valueOf after all, because it permits caching of small values.
|
 |
Catherine austin
Ranch Hand
Joined: Jul 20, 2011
Posts: 43
|
|
|
Yeah , i got u. Thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
Well done
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
I was mistaking it for the Integer.valueOf(int) method.
Agree that for turning a String to an int parseInt() is a lot better.
|
 |
 |
|
|
subject: Unexpected ArrayIndexOutOf BoundsException.
|
|
|