Results 1 to 3 of 3
  1. #1
    Trooper88 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Null on 2D Array

    I know this is probably a very simple solution, but im very new to Java... im just switching over from C

    My problem is i get a "Null" exception on my gameOn 2D array in the display method... can anyone help?

    The display is a method that supposed to print a 6 by 7 grid of X's with numbers 1 through 7 at the bottom

    Thanks

    Java Code:
    public class connect4 
    {
        // instance variables
        private String[][] gameOn;
    
        /**
         * Constructor for objects of class connect4
         */
        public connect4()
        {
            // initialise instance variables
            String[][] gameOn = new String[6][7];
        
        }
    
    
        public void display()
        {
    
            for( int x = 0; x <= 5; x++)
            {
                  for( int y = 0; y <= 6; y++)
                  {
                    gameOn[x][y] = "X";
                  }
            }
            
            for(int x = 0; x <= 5; x++)
            {
                   int y = 0;
                  System.out.println("  " + gameOn[x][y] + "  "  + gameOn[x][y+1] +"  " +  gameOn[x][y+2] +"  " +  gameOn[x][y+3] +"  " +  gameOn[x][y+4] +"  " +  gameOn[x][y+5] +"  " +  gameOn[x][y + 6]); 
            }
        
            System.out.println("  " + 1 + "  "  + 2 +"  " +  3 +"  " +  4 +"  " +  5 +"  " +  6 +"  " +  7); 
        
        }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Java Code:
       public connect4()
        {
            // initialise instance variables
            String[][] gameOn = new String[6][7];
        
        }
    remove the String[][] :D

  3. #3
    Trooper88 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you so much, you have no idea how long ive stared at this code haha

Similar Threads

  1. Array null values
    By blug in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 06:48 AM
  2. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  3. How to get null values stored in array
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 10:29 PM
  4. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 08:15 PM
  5. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 10:42 PM

Posting Permissions

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