0

guys I'm trying to build Tic-Tac-Toe from scratch by using java, I'm aware it's not may be perfect but I will try anyway! I wrote a function that should printing the board when I'm calling it but it's gives me NullPointerException idk why tbh

import java.io.*;
import java.util.*;
class Task_3{
    static char[][] board;
    static void checkWinner(){
        String v=null;
        for(int p=0;p<8;p++){
            switch(p){
                case 0:
                    v=String.valueOf(board[0][0]+board[0][1]+board[0][2]);
                    break;
                case 1:
                    v=String.valueOf(board[1][0]+board[1][1]+board[1][2]);
                    break;
                case 2:
                    v=String.valueOf(board[2][0]+board[2][1]+board[2][2]);
                    break;
                case 3:
                    v=String.valueOf(board[0][0]+board[1][0]+board[2][0]);
                    break;
                case 4:
                    v=String.valueOf(board[0][1]+board[1][1]+board[2][1]);
                    break;
                case 5:
                    v=String.valueOf(board[0][2]+board[1][2]+board[2][2]);
                    break;
                case 6:
                    v=String.valueOf(board[0][0]+board[1][1]+board[2][2]);
                    break;
                case 7:
                    v=String.valueOf(board[0][2]+board[1][1]+board[2][0]);
                    break;
            }
            if (v.equals("XXX")){
                System.out.println("X is The Winner!");
            }else if(v.equals("OOO")){
                System.out.println("O is The Winner!");
            }else{
                System.out.println("Tie");
            }
        }
    }
    static void printBoard()
    {
        System.out.println("|---|---|---|");
        System.out.println("| " + board[0][0] + " | "
                           + board[0][1] + " | " + board[0][2]
                           + " |");
        System.out.println("|-----------|");
        System.out.println("| " + board[1][0] + " | "
                           + board[1][1] + " | " + board[1][2]
                           + " |");
        System.out.println("|-----------|");
        System.out.println("| " + board[2][0] + " | "
                           + board[2][1] + " | " + board[2][2]
                           + " |");
        System.out.println("|---|---|---|");
    }
    public static void main (String[] args) {
    int pos=0, xoro=1;
    Scanner sc = new Scanner(System.in);
    char[][] board = new char[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;
    printBoard();
    pos = sc.nextInt();
    switch(pos){
        case 1:
            if (xoro % 2 == 0)
                board[0][0]='O';
                else
                    board[0][0]='X';
                xoro++;
                for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                System.out.println(board[i][j]);
            break;
        case 2:
            if (xoro % 2 == 0)
            board[0][1]='O';
                else
                    board[0][1]='X';
                xoro++;
            break;
        case 3:
            if (xoro % 2 == 0)
            board[0][2]='O';
                else
                    board[0][2]='X';
                xoro++;
            break;
        case 4:
            if (xoro % 2 == 0)
            board[1][0]='O';
                else
                    board[1][0]='X';
                xoro++;
            break;
        case 5:
            if (xoro % 2 == 0)
            board[1][1]='O';
                else
                    board[1][1]='X';
                xoro++;
            break;
        case 6:
            if (xoro % 2 == 0)
            board[1][2]='O';
                else
                    board[1][2]='X';
                xoro++;
            break;
        case 7:
            if (xoro % 2 == 0)
            board[2][0]='O';
                else
                    board[2][0]='X';
                xoro++;
            break;
        case 8:
            if (xoro % 2 == 0)
            board[2][1]='O';
                else
                    board[2][1]='X';
                xoro++;
            break;
        case 9:
            if (xoro % 2 == 0)
            board[2][2]='O';
                else
                    board[2][2]='X';
                xoro++;
            break;
    }
    }
}

and this is the error I got

|---|---|---|
Exception in thread "main" java.lang.NullPointerException
    at Task_3.printBoard(Task_3.java:46)
    at Task_3.main(Task_3.java:72)

Process completed.

tbh I don't know what seems what be the problem also I'm not that professional in java so sorry if it's was to basic. Thank you guys.. have a nice day.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
idkjusthpme
  • 39
  • 1
  • 5
  • Do you initialize the board array anywhere before using it? – Hovercraft Full Of Eels Jun 26 '21 at 20:51
  • btw guys its not complete yet but I will do it after finding a solution for this problem – idkjusthpme Jun 26 '21 at 20:51
  • 2
    Ah, you are *shadowing* the board variable by declaring it twice. Yes, the variable *with the same name* is initialized in the main method, but this is not the same board variable that you are using elsewhere. – Hovercraft Full Of Eels Jun 26 '21 at 20:52
  • 1
    More importantly, you absolutely *must* learn the general steps used to debug any NullPointerException (NPE) because if you continue programming, you will encounter these beasts again and again. Please read fully the answer in the duplicate used to close your question. Feel free to ask any questions about that answer if it confuses you. – Hovercraft Full Of Eels Jun 26 '21 at 20:53
  • So, in the main method, change `char[][] board = new char[3][3];` to `board = new char[3][3];` Do you see how these lines are different? The second example removes the `char[][] board` variable declaration statement. More importantly, do you understand why this matters? Look up variable shadowing for more on this. – Hovercraft Full Of Eels Jun 26 '21 at 20:55
  • yes I did before calling the printBoard(), that's why im confused about it – idkjusthpme Jun 26 '21 at 20:55
  • No, you did not. Please read or re-read my comments – Hovercraft Full Of Eels Jun 26 '21 at 20:56
  • You're welcome, but again most important, do you see why the code changes that I recommended in [this comment](https://stackoverflow.com/questions/68146136/nullpointerexception-while-calling-function-that-print-values-of-full-multidimen?noredirect=1#comment120442039_68146136) were made? And do you understand now why the NullPointerException was thrown by your code? – Hovercraft Full Of Eels Jun 26 '21 at 21:01
  • Again, this variable: `static char[][] board;`, is *not the same* as this one: `char[][] board = new char[3][3];` You have to really understand this to move forward. – Hovercraft Full Of Eels Jun 26 '21 at 21:02
  • yes it did, there is a different between initializing and declaring & also I need to read the explanation about NPE you recommended to me. thanks – idkjusthpme Jun 26 '21 at 21:05

0 Answers0