Results 1 to 10 of 10
  1. #1
    Gerrburge is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default trying to learn enums and arrays

    I am trying to learn how to use arrays and enums. I am just trying to populate the grid with 'empty' in my class. Then I am just trying to print out the values with testArray. But I am obviously missing something VERY important. Can anyone help?
    __the class
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.net.*;
    import java.applet.*;

    public class GTicTacToeL
    {

    public enum Square
    {empty,x,o}
    Square board[][];
    {
    for ( int row = 0; row < 3; ++row )
    {
    for ( int column = 0; column < 3; ++column )
    {
    board[row][column] = Square.empty;
    }
    }
    }

    }
    the printing
    public class testArray
    {
    public static void main (String[] args)
    {
    GTicTacToeL testArray = new GTicTacToeL();

    {
    for (int x = 0; x<3; ++x)
    {
    for (int y = 0; y<3; ++y)
    {
    System.out.println(square[x][y]);
    }
    }

    }
    new to this forum and very new to Java...so any advise is good advise.
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Gerrburge View Post
    I am trying to learn how to use arrays and enums. I am just trying to populate the grid with 'empty' in my class. Then I am just trying to print out the values with testArray. But I am obviously missing something VERY important. Can anyone help?
    Your code tags need to be edited as you have a pair above and a pair below the block of code. You need one pair of tags with your code block between the pair.

    Regarding your problem, please tell us what is or isn't working, what errors you may be getting. Please read the link in my signature on smart questions for tips on how to make your question more answerable.

  3. #3
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

  4. #4
    Gerrburge is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default help with enum and arrays

    I am trying to understand 2 dimensional arrays and enums. I have already been to the Java tutorial sites. I tried to code some on my own with this class
    Java Code:
    public class GTicTacToeL
      {
    
        public enum Square
        {empty,x,o}
         Square board[][];
         {
            for ( int row = 0; row < 3; ++row )
                {
                    for ( int column = 0; column < 3; ++column )
                        {
                 board[row][column] = Square.empty;
                        }
                }
         }
         
    }
    and this program to just print what was in the array
    Java Code:
    public class testArray
    {
    public static void main (String[] args)
        {
             GTicTacToeL testArray = new GTicTacToeL();
    
        {
         for (int x = 0; x<3; ++x)
             {
                for (int y = 0; y<3; ++y)
                {
                    System.out.println(square[x][y]);
                }
             }
    
         }
        }
    }
    But I get this error when compiled.
    Java Code:
    run:
    Exception in thread "main" java.lang.NullPointerException
            at GTicTacToeL.<init>(GTicTacToeL.java:18)
            at testArray.main(testArray.java:14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    can anyone help?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    That error message does not come from the code you posted (which does not compile).

    --------------

    A couple of points of style: Start class names with an upper case letter and methods and variables with a lower case one. As a side effect this will mean you don't use the same word as both a class and a variable as you do now with testArray. Also don't put braces around for loops - but use braces for the body of the loop as you are doing.

    Java Code:
    {
        for (int x = 0; x<3; ++x)
            {
                for (int y = 0; y<3; ++y)
                {
                    System.out.println(square[x][y]);
                }
            }
    
        }
    }
    should just be

    Java Code:
    for (int x = 0; x<3; ++x)
        {
            for (int y = 0; y<3; ++y)
            {
                System.out.println(square[x][y]);
            }
        }
    }

    (Using spaces rather than tabs to indent will result in a more consistent and predictable result when posting on the internet.)

  6. #6
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
     
     System.out.println(square[x][y]);
    Where is that lower case square initialized?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    As has been pointed out this doesn't compile, but:
    Java Code:
    Square board[][];
    This is never initialised so is always null.

  8. #8
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by hosscomp View Post
    Java Code:
     
     System.out.println(square[x][y]);
    Where is that lower case square initialized?
    Quote Originally Posted by Tolls View Post
    As has been pointed out this doesn't compile, but:
    Java Code:
    Square board[][];
    This is never initialised so is always null.
    Okay, but the println statement is trying to print square, not board.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Sorry, that wasn't directed at you.

    Assuming there is (somewhere) some code that actually compiles that looks something like the above code then the NPE is coming from:
    Java Code:
    board[row][column] = Square.empty;
    in the constructor of GTicTacToeL, since that's what the exception is telling us.

    And that NPE is because "board" is never initialised.

  10. #10
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

Similar Threads

  1. How did you learn?
    By code_newbie in forum New To Java
    Replies: 17
    Last Post: 12-25-2010, 02:25 AM
  2. Returning flags from enums
    By willemien in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 08:37 AM
  3. declaring problems with enums
    By jackrulesok in forum New To Java
    Replies: 10
    Last Post: 04-30-2010, 11:16 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. why we are using enums in Java?
    By manish.anchan in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 05:41 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
  •