Results 1 to 4 of 4
  1. #1
    ieoj31 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Preventing exceptions

    Hi,

    I'm having some trouble preventing various exceptions in an implementation of Conway's Game of Life. Here's the code.

    Java 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];
                }
            }
        }      
    }
    The exception that I'm getting with this is an arrayindexoutofboundsexception: -1 at line 52

    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!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ieoj31 View Post
    The exception that I'm getting with this is an arrayindexoutofboundsexception: -1 at line 52

    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?
    You have to check the values for row and col, e.g. if row == 0 you can't refer to cell[row-1]. That complicates all the conditions in the various if statements somewhat.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ieoj31 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    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?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ieoj31 View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 10-30-2010, 06:56 PM
  2. Preventing local copies of fields
    By Steve11235 in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-05-2009, 05:51 AM
  3. preventing access/locking a folder?
    By solris in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 12:20 AM
  4. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM
  5. Replies: 2
    Last Post: 08-10-2007, 12:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •