I'm trying to make a server-client based TicTacToe game, and here I'm just starting off with the raw, raw basics.
The "client" will be executed in a thread. So, it implements runnable and has a Run() function.
I want to print the board during this run function.....but it claims that my board hasn't been initialized!!!
I've bolded the "board.printBoard()" command in the two parts that I have it in my code. The first part is part of the constructor, and it prints the board with no problem.
Once I enter the run() function, however, it started throwing exceptions all of a sudden for no apparent reason. What's going on here?
Here's the error message:
HTML Code:Let's try to print the board Exception in thread "main" java.lang.NullPointerException at TTTClient.run(TTTClient.java:53) at TTTClient.<init>(TTTClient.java:47) at TTTClient.main(TTTClient.java:27)
And here's the code.
Thanks in advance for the help.
import java.io.*; import java.net.*; import java.util.concurrent.*; import java.util.Scanner; public class TTTClient implements Runnable { private char mySymbol; private boolean myTurn; private final char X_SYMBOL = 'X'; private final char O_SYMBOL = 'O'; private Board board; private Socket connection; private DataInputStream inpFromServer; private DataOutputStream outToServer; public static void main(String[] args) { TTTClient player = new TTTClient(); } public TTTClient() { Board board = new Board(); [B]board.printBoard();[/B] try { connection = new Socket("localhost", 9821); inpFromServer = new DataInputStream(connection.getInputStream()); outToServer = new DataOutputStream(connection.getOutputStream()); } catch (IOException ex) { System.err.println(ex); } run(); } public void run() { System.out.println("Let's try to print the board"); [B]board.printBoard();[/B] System.out.println("The board should have been printed"); } }