aspose file tools*
The moose likes Beginning Java and the fly likes AOOB exception in my CODE-PLEASE HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "AOOB exception in my CODE-PLEASE HELP!" Watch "AOOB exception in my CODE-PLEASE HELP!" New topic
Author

AOOB exception in my CODE-PLEASE HELP!

Danielle Honigman
Greenhorn

Joined: Jun 04, 2002
Posts: 3
Hi There!
I am a newbie to java (and programming in general) and have been working on programming a simple text-based version of Conway's Game of Life. Everything was working perfectly when I had a fixed 10 by 10 array, but then I decided it would be more interesting to have static variables x and y for the length and width of the array so that you could easily change the dimensions of the playing board. So, I did that, and when I initialized them to any value (>= 10 since my start patterns were made with a 10 by 10 in mind) the program worked fine and printed boards of the indicated size. HOWEVER, when I extended that a bit and added some code prompting the user to indicate the desired dimensions, the program compiled correctly but then when it was running I got an ArrayIndexOutOfBoundsException and I can't seem to fix it. I know the error most
likely lies somewhere in the user-prompting code, but everything seems fine to me. I think for some reason the JVM is reading whatever number
I type in first (which is when you're prompted to choose a start pattern between 1 and 7) as the dimensions, but I'm not sure why. I have been tearing my hair out trying to debug this program, so I would GREATLY appreciate any help anyone could provide!
My code would probably be helpful =-P so here it is...I hope it's readable after all this cutting and pasting into this forum:

Thanks in advance for your help!
[ June 04, 2002: Message edited by: Dirk Schreckmann ]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Danielle,
I would make some small changes in the way your program is laid out. I would start with creating a method to get the valid integer. It will make your code look much cleaner:

The using that routine, you can request some information from the user. One big change to your program will have to be the use of initialization when declaring the variables. This must stop because once an array in Java is initialized its' size is fixed from then on. Also Java initializes all arrays with zero values so you don't need to do that explicitly.
Another few changes:
1. Ask user for size of board first so we can initialize the array before we are going to use it.
2. Don't use for loop to pause for a certain amount of time. Loops are dependent on processor speed and don't care about time clocks. Use the sleep method of Thread which takes a parameter of milliseconds to sleep for.
Now the modified main method:

Enjoy,
Manfred.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

The problem is your array CurrentGen is never being initialized... so when the numOfNeighbors() method is executed and tries to access one of the elements in the uninitialized array -- it throws an ArrayIndexOutOfBounds Exception.

So you need to move the initialization of your CurrentGen[] array to AFTER you've assigned values to x and y.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jessica,
If it were only that simple. As I have stated in my post:
Array size is immutable
What does that mean? Well it means that if I have a static variable (gets declared and initialized at class loading time):
static int[][] array = new int[0][0];
I can never change the size of array.
Therefore, the only solution is to stop doing the initialization in the declaration part and do it inside the constructor after the correct sizes are specified.
Regards,
Manfred.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

.... caught me ...
Danielle Honigman
Greenhorn

Joined: Jun 04, 2002
Posts: 3
Hi Manfred,
First off, thanks so much for taking all the time and effort to right such a helpful reply! I really like your suggestion of creating a method to get a valid integer from the user, because that makes things a lot easier since everything I'm getting from the user happens to be integers (since that's all I really know the java syntax for as of now). Just a question about that..is there any reason why you create a string buffer object (constructor method?--sorry I am still very new to object-oriented concepts) to display the pattern options on the screen? Oh..good point about the pausing..I didn't even think about the fact that a for loop depends on procesing speed..I was sure there had to be a pause method but I was just using that for the time beign..thanks so much for the info! okay last but def most important: when i modified my code with your corrections i ran into the problem that CurrentGen[][] and NextGen[][] were not being recognized in my NextGeneration() and NumNeighbors() methods...how exactly do I fix that? I tried making C.G[][] and N.G[][] static int arrays rather than just int arrays like you had, but of course that gave me an "invalid start" error because u can only declare static variables at the beginning of the class, so I'm not sure how to resolve that . If you have any ideas I would truly appreciate it. Thanks again!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
HI Danielle,
Here is the complete code that works for me without any errors. Take a look at it and compare with what you have.

Regards,
Manfred.
Danielle Honigman
Greenhorn

Joined: Jun 04, 2002
Posts: 3
ah, very stupid mistake...I forgot to declare the static arrays in the beggining of the class and just did the declaration and initiation in the method, which explains why the other methods weren't recognizing the arrays (since they weren't static). Thanks!!! =)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AOOB exception in my CODE-PLEASE HELP!