aspose file tools*
The moose likes Java in General and the fly likes ArrayIndexOutOfBounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayIndexOutOfBounds Exception" Watch "ArrayIndexOutOfBounds Exception" New topic
Author

ArrayIndexOutOfBounds Exception

Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Hi, I have a problem in accesing array elements. Please look at the code.


Based on the algorithm I have choosen, the calculated value of "d" should fall with in the range of array size. The same code was working when I use it in Java script.
Code I used in Jscript:


My question is when I used in Jscript I do not get any array index out of bound exception, but in case of Java I do get the exception. Whats wrong with the code? Can somebody share your thoughts? Thanks

[ inserted line breaks for readability - Jim ]
[ December 22, 2006: Message edited by: Jim Yingst ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

This looks strange to me:You calculate this value d, which from your comments is intended to be an array index, but then you ignore it and use some other value.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Hey iam sorry, it was not 'r' its 'd' only. I misplaced 'd' with 'r'.
Here is corrected one.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

The stack trace for the ArrayIndexOutOfBoundsException will tell you which index it is trying to access. With that information, you can tell which element is being accessed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
ya, i know that. My question is is there any difference in saying


AND


In the first case 'c' value is within the range of array, where as its not in second case.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

In the first case 'c' value is within the range of array, where as its not in second case.


You are assuming that it is the index that is wrong... To confirm that you need to print out the indexes for both operations -- working and not working to see if they are the same.

Another option is that the array could be wrong. Is the other array also a string array? or is it a byte array?

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayIndexOutOfBounds Exception