I'm working on programming my own tic tac toe game in javafx, but seem to have stumbled across some fundamental mechanic that I don't understand in dealing with classes. I tried to simplify my problem for this question. I just want to create a Game object containing a two dimensional string array and then print off the strings from that array through another class' method. I get a null pointer exception when I run this code. Any help would be greatly appreciated. Cheers.
package testing;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Game g = new Game();
g.addMove("X", 1, 0);
g.addMove("O", 0, 1);
GUI.printBoard(g);
}
public static void main(String[] args) {
launch(args);
}
}
package testing;
public class Game {
private String[][] board;
public Game() {
this.board = new String[3][3];
}
public String[][] getBoard() {
return this.board;
}
public void addMove(String s, int row, int col) {
this.board[row][col] = s;
}
}
package testing;
public class GUI {
public static void printBoard(Game g) {
String[][] board = g.getBoard();
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row][col].equals("X"))
System.out.println(board[row][col]);
else
if (board[row][col].equals("O"))
System.out.println(board[row][col]);
}
}
}
}