Results 1 to 4 of 4
Thread: Preventing exceptions
- 11-15-2010, 12:25 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Preventing exceptions
Hi,
I'm having some trouble preventing various exceptions in an implementation of Conway's Game of Life. Here's the code.
The exception that I'm getting with this is an arrayindexoutofboundsexception: -1 at line 52Java Code:import java.util.*; public class Life { private final boolean[][] cells; public static void main( String[] args ) { Life generation = new Life( 70 ); for (int i = 0; i != 100; i++) { System.out.println( generation ); generation.next(); } } // Constructors // Initialise size * size grid with random cells. public Life( int size ) { final Random rand = new Random( ); cells = new boolean[ size ][ ]; for (int row = 0; row < size; row ++) { cells[ row ] = new boolean[ size ]; for (int col = 0; col < size; col ++) { cells[ row ][ col ] = (rand.nextInt( 2 ) == 0); } } } // Public methods and helper methods. @Override public String toString( ) { String result = ""; for (int row = 0; row < cells.length; row ++) { final boolean[] column = cells[ row ]; for (int col = 0; col < column.length; col ++) { result = result + (column[ col ] ? "x" : "."); } result = result + "\n"; } return result; } public void next() { //apply the rules to each cell //output the new grid //counter to count number of live cells boolean[][] newGrid; newGrid = new boolean[cells.length][]; int counter = 0; for (int row = 0; row < cells.length; row++) { cells[ row ] = new boolean[ cells.length ]; for (int col = 0; col < cells[row].length; col++) { if (cells[row + 1][col - 1] == true) { counter++; } if (cells[row - 1][col - 1] == true) { counter++; } if (cells[row - 1][col] == true) { counter++; } if (cells[row - 1][col + 1] == true) { counter++; } if (cells[row][col - 1] == true) { counter++; } if (cells[row][col] == true) { counter++; } if (cells[row][col + 1] == true) { counter++; } if (cells[row + 1][col] == true) { counter++; } if (cells[row + 1][col + 1] == true) { counter++; } if (counter > 2 && counter < 3) { newGrid[row][col] = true; } else { newGrid[row][col] = false; } cells[row][col] = newGrid[row][col]; } } } }
I know this is related to the various [row - 1] and [col - 1] references I have and how they will sometimes be null, but how do I resolve this?
Thanks in advance!
- 11-15-2010, 12:41 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 11-15-2010, 01:14 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
I think I've sorted that out, but i'm still getting a null pointer exception at line 92 (newGrid[row][col] = false;)
Any idea where this is coming from?
- 11-15-2010, 01:33 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Yep, newGrid is a two dimensional array, i.e. newGrid[i] is the i-th row and it contains all the column values of that row (it's a one dimensional array). You only allocated the row references but none of the columns of your two dimensional array (see above your loop). All the one dimensional arrays representing the columns are null. Hence the exception.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Preventing errors when typing a letter into a String
By doymand in forum New To JavaReplies: 5Last Post: 10-30-2010, 06:56 PM -
Preventing local copies of fields
By Steve11235 in forum Threads and SynchronizationReplies: 2Last Post: 08-05-2009, 05:51 AM -
preventing access/locking a folder?
By solris in forum New To JavaReplies: 1Last Post: 06-29-2009, 12:20 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 07:12 PM -
Preventing inserted text from becoming colored from previous style
By jkhoa in forum AWT / SwingReplies: 2Last Post: 08-10-2007, 12:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks