-2

I keep getting:

Exception in thread "main" java.lang.NullPointerException
at TTTPlayer.setBoard(TTTPlayer.java:55)    
at TTTPlayer.inputMethod(TTTPlayer.java:35)     
at TTTPlayer.main(TTTPlayer.java:23)

My program is a tictactoe program and I am trying to asign the value the person inputs to the corresponding array index, which then can be printed out. I know I do not have a flip flopper yet for the switching of the players. I can't figure this out though.

import java.util.*;

public class TTTPlayer {

    public static String[][] board;
    public static String currentPlayer;

    public TTTPlayer() {
        currentPlayer = "x";
        board = new String[3][3];
        board[0][0] = "1";
        board[0][1] = "2";
        board[0][2] = "3";
        board[1][0] = "4";
        board[1][1] = "5";
        board[1][2] = "6";
        board[2][0] = "7";
        board[2][1] = "8";
        board[2][2] = "9";
    }

    public static void main(String[] args) {
        inputMethod();
    }

    public static void inputMethod()
    {
        TicTacToeMain.printBoard();
        Scanner Input = new Scanner(System.in);
        System.out.println("Enter what square you would like to place in.");
        String choice = Input.nextLine();

        if(choice.equals("1")||choice.equals("2")||choice.equals("3")||choice.equals("4")||choice.equals("5")||choice.equals("6")||choice.equals("7")||choice.equals("8")||choice.equals("9"))
        {   
            setBoard(choice);

        }   
        else
        {   
            System.out.println("That is not a valid choice, please retry.");
            inputMethod();

        }   

    }
    public static void setBoard(String choice)
    {
        String choice1 = choice;

         for (int i = 0; i < 3; i++) {


            for (int j = 0; j < 3; j++) {

               if(board[i][j].equals(choice1)){
                  board[i][j].equals(currentPlayer);
                  printBoard();

               }

            }
        }
        //System.out.println(choice1);  
    }

    public static void printBoard()
    {  
       System.out.println(board[0][0]+" | "+board[1][0]+" | "+board[2][0]);
       System.out.println("——|———|——");
       System.out.println(board[0][1]+" | "+board[1][1]+" | "+board[2][1]);
       System.out.println("——|———|——");
       System.out.println(board[0][2]+" | "+board[1][2]+" | "+board[2][2]);

    }
}
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • 1
    @henry I also originally closed this as a duplicate, but the root cause is caused by actually not instantiating his class from `main` I could not see this cause answered in the dupe. – Scary Wombat Nov 21 '16 at 04:56
  • @ScaryWombat I still think it's a duplicate. There are way to many reasons why a variable may not be initialized to list them all. The root problem is usually easy to diagnose either by inspection or by firing up the debugger. – Henry Nov 21 '16 at 20:11

1 Answers1

2

In main you need to instantiate a TTTPlayer Object. Also make the classes methods non-static.

public static void main(String[] args) {
    TTTPlayer player = new TTTPlayer ();
    player.inputMethod();
}

and

public static void inputMethod() -> public void inputMethod()
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64