• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding an error  RSS feed

 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm executing a class which is compiling but not running. When I try to execute the class I get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 49
at TestTest.performStuff(TestTest.java:102)
at TestTest.stuff(TestTest.java:63)
at TestSay.main(TestTest.java:26)

at line 26 I have a call from inside main to the stuff method, which returns the value of a variable.

int indStuff = stuff(args[0];

at line 63 in my stuff() method I have a call to my performStuff method, which accepts indStuff as an argument, does its thing to modify indStuff, and returns it back to stuff().

int indStuff = performStuff(indStuff);

and at line 102 I'm assigning a value to an index in an instance array using another instance array that holds constant values.

firstArray[indstuff] = secondArray[x];


I'm not quite sure what I'm doing wrong here. Any help would be appreciated, and I could send the entire code in a private message if more information is necessary.

Nathan
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Check the x value, it should not exceed ( secondArray.length - 1 )

hth,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, remember that array indexing begins with zero. So if an array's length is x, then its indexes will range from zero through x-1. An index of x will be out of bounds.
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses. I finally figured out what was happening. It was a problem in a very lengthy series of if/else/for statements. I ended up decrementing something in two different statements that ended up returning true.

If I can give advice to someone starting to code, comment your code heavily. For me, at least, it helps me from getting lost and remembering why that "brilliant" idea I had was so brilliant in the first place.

Nathan

And for those who are new, the error means what it says, the index is out of bounds for the array referenced... go through and make sure you aren't decrementing or incrementing the index below or passed its values. 0 to 1-length.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!