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
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
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
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.
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?