aspose file tools*
The moose likes Beginning Java and the fly likes Unexpected ArrayIndexOutOf BoundsException. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected ArrayIndexOutOf BoundsException." Watch "Unexpected ArrayIndexOutOf BoundsException." New topic
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: 10523
    
    9

What args did you pass?
Can you post the stack trace?


[How to ask questions] [Donate a pint, save a life!] [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: 39869
    
  28
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: 39869
    
  28
Well done
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
I was mistaking it for the Integer.valueOf(int) method.

Agree that for turning a String to an int parseInt() is a lot better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unexpected ArrayIndexOutOf BoundsException.