Results 1 to 10 of 10
Thread: trying to learn enums and arrays
- 01-29-2011, 12:03 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
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
-
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.
- 01-29-2011, 12:15 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
- 01-29-2011, 01:05 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
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; } } } }
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]); } } } } }
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)
- 01-29-2011, 02:24 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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]); } } } }
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.)
- 02-02-2011, 12:01 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Java Code:System.out.println(square[x][y]);
- 02-02-2011, 09:24 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
As has been pointed out this doesn't compile, but:
Java Code:Square board[][];
- 02-02-2011, 03:37 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
- 02-02-2011, 03:49 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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;
And that NPE is because "board" is never initialised.
- 02-02-2011, 03:54 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
How did you learn?
By code_newbie in forum New To JavaReplies: 17Last Post: 12-25-2010, 02:25 AM -
Returning flags from enums
By willemien in forum New To JavaReplies: 5Last Post: 05-26-2010, 08:37 AM -
declaring problems with enums
By jackrulesok in forum New To JavaReplies: 10Last Post: 04-30-2010, 11:16 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 02:40 AM -
why we are using enums in Java?
By manish.anchan in forum New To JavaReplies: 7Last Post: 01-08-2010, 05:41 PM
Bookmarks