2

For a project at University, I have to create a game of Tic Tac Toe.

I have this for loop with if statements to search through the 2D array of 3x3 size, and return if it's either X or O (enum). That results in showing which side has won the game.

However, the problem I have is that if the 2D array is not complete, as in if all the 9 boxes are not filled with X or O, the method shows a NullPointerException.

Edit: I have to add that I require the empty grid to be null as few other unit tests assume grid[][] is initialized as null.

Error:

Exception in thread "main" java.lang.NullPointerException
at TicTacToeImplementation.whoHasWon(TicTacToeImplementation.java:80)
at ApplicationRunner.main(ApplicationRunner.java:24)

Code:

public enum Symbol {
    X, O
}

private Symbol winner;

public Symbol whoHasWon() {

    for (Symbol xORo : Symbol.values()) {

        if ((grid[0][0].equals(xORo) &&
                grid[0][1].equals(xORo) &&
                grid[0][2].equals(xORo))) {
            winner = xORo;
            isGameOver = true;

            break;
        } else if ((grid[1][0].equals(xORo) &&
                grid[1][1].equals(xORo) &&
                grid[1][2].equals(xORo))) {
            winner = xORo;
            isGameOver = true;

            break;}
           else if { //Code carries on to account for all 8 different ways of winning

        } else {

            isGameOver = true;
        }
    }

    return winner;
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
Abdullah
  • 51
  • 5
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Glains Aug 31 '18 at 20:24
  • 1
    Where is `grid` initialized? But, since enum values can (and should) be compared using `==`, change code to `grid[0][0] == xORo`, so a blank (aka `null`) cell will compare false, instead of throwing NPE. – Andreas Aug 31 '18 at 20:26
  • You can check if not null before the equals method. Other way is fill all lines with something like any other character comma dot exc. – drowny Aug 31 '18 at 20:31

3 Answers3

0

You can use multiple ways to ignore the "null" exception with an empty array.

The 1st way is to fill it with a different default symbol such as E. So when you initialize your arry at the beginning, instead of making it all empty and null, you can fill it with E's

for(int i=0;i<=2;i++){
    for(int k=0;k<=2;k++){
        grid[i][k] = "E";
    }
}

Add this to beginning to fill it with E's first instead of nulls.

Another method is to find how to ignore the nulls using try or the following methods that can be found in this linkhttps://www.javacodegeeks.com/2012/06/avoid-null-pointer-exception-in-java.html:

I won't be going into it because I believe the 1st method is easier to use and implement. However, depending on your requirements for your assignment, I would look at both just to be sure.

Hope this helps, Good luck!

Edward Dan
  • 134
  • 1
  • 10
  • I'm required to keep the null, as I also have some unit testing that requires initial values to be null. I'll have a look at the second one to avoid the null point exception. Also, it's only my 3rd week learning Java from the beginning so i'll probably need bit more direction – Abdullah Aug 31 '18 at 20:37
  • ha, only my 2nd week of learning java... but anyways I would check out that website I linked or just search up "ignore null pointer exception in java" in google – Edward Dan Aug 31 '18 at 20:47
0

You can change the comparasion of String.The code may be like this ;

public Symbol whoHasWon() {

    for (Symbol xORo : Symbol.values()) {

        if ((grid[0][0] == xORo.name() &&
                grid[0][1] == xORo.name() &&
                grid[0][2] == xORo.name())) {
            winner = xORo;
            isGameOver = true;

            break;
        } else if ((grid[1][0] == xORo.name() &&
                grid[1][1] == xORo.name() &&
                grid[1][2] == xORo.name())) {
            winner = xORo;
            isGameOver = true;

            break;}
        else if { //Code carries on to account for all 8 different ways of winning

        } else {

            isGameOver = true;
        }
    }

    return winner;
}

Enum like your's implemented

public enum Symbol{
        X, O
        }
    }
drowny
  • 2,067
  • 11
  • 19
0

As stated in this post, you can use either equals() or == to compare enums but using == is null safe while equals() isn't.

So basically, just write your checks like this:

if (grid[0][0] == xORo &&
    grid[0][1] == xORo &&
    // etc.

However, if you want to use the equals() method, you could just write a method that checks for null then compares the two values and returns the result:

public boolean isEqual(Symbol s1, Symbol s2) {
    if (s1 != null && s1.equals(s2)) {
        return true;
    }
    return false;
}

You could then call the isEqual() method like this:

if (isEqual(grid[0][0], xORo) &&
    isEqual(grid[0][1], xORo) &&
    // etc.
0xCursor
  • 2,242
  • 4
  • 15
  • 33