• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBounds present, but not expected, when run inside another class  RSS feed

 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am stuck on a problem and need some help. I have written (and am debugging) a program that generates a number, puts that number in an array, and then compares a user guess to that number. The point I am stuck at is when the computer generates the number, the array I am putting it in is throwing an exception that the index is out of bounds. That must mean that (to my knowledge) the index is below zero. But when I run the program separately and check the index, it starts at 0. The method in question runs independently and generates the array without throwing the exception, but when it's called from the 'mother program' the exception is thrown. It points to the line in the code where the array is being filled inside a loop here:


I have checked all the variables involved and none of them is the culprit. Is there something to do with making the array a static variable so that when it's called from outside it's original class it maintains itself? This is exactly what we are learning in class right now, which is why I ask, as I am still a little fuzzy on what 'static' means exactly.

Here's the code for the 'mother program'...I am sure there are errors in it besides what I am asking about, but the program runs as it should up until the point in question:


And here's the code for the other two classes I use (if that helps):







Anyway, I love the forum and thanks for taking a look at this. Any thoughts?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james aggeles wrote:throwing an exception that the index is out of bounds. That must mean that (to my knowledge) the index is below zero.


If you have an array of size N, then its indices are 0..N-1. So if the index you try to use is < 0 or >= N, you'll get ArrayIndexOutOfBoundsException.

If you can't figure out why you're getting it, then add a bunch or print statements to your code so you can see what index you're trying to use and where it's coming from.

Also, as a tip for future questions, that's way too much code to post. Some folks here (me included) won't even bother to glance at code that long. You should be able to produce an SSCCE(←click) that has just enough code to show the problem you're having, and nothing else, no code not directly related to the problem.

Good luck!
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Sorry for all the code, I didn't realize. I will work on it and get back to you.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for welcoming me

So I ran some tests, and the indexes of the array are all correct, nothing above or below the right values. The array is receiving the right numbers and storing them properly, and when I call the method from inside the 'engine' main() method, it responds properly. It only seems to happen if I call it from the bagel class I posted. Does it have anything to do with constructors, like do I need to create an engine constructor, and if so, what should it look like? Thanks guys and gals.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so, some progress: when I delete the last section in the engine class that reads: byte[] secretNumber = new byte[numDigits]; so that it reads:
byte[] secretNumber; I get a null pointer exception, but when I leave it as is, I get the ArrayIndexOutOfBounds exception. numDigits is being read in properly, because when I put a print statement inside the method, it prints the right value for numDigits...

Does any of this help?
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out I was initializing the arrays too early. thanks for the help though, and for the guidance on forum etiquette. Regards!
 
Ryan Steelflex
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show where the correction is needed because I cannot find it :/
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!