0

I am trying to design a simple 3x3 sliding puzzle game. The only tile you can move is a blank tile and this can be swapped with any of its neighbouring tiles. There is currently no final goal for the game but I have had trouble implementing the puzzle and being able to swap tiles. I keep getting java null pointer exception when I test out my swap methods and can't seem to fix this. I am aware what this error is but can't see why it occurs in my code.

public class Game() {

    public Node[][] buildPuzzle() {

        Node[][] puzzle = new Node[3][3];

        Node One = new Node("1", 0, 0, null, puzzle[0][1], null, puzzle[1][0]);
        Node Two = new Node("2", 1, 0, null, puzzle[1][1], puzzle[0][0], puzzle[2][0]); 
        Node Three = new Node("3", 2, 0, null, puzzle[2][1], puzzle[1][0], null);

        Node Four = new Node("4", 0, 1, puzzle[0][0], puzzle[0][2], null, puzzle[1][1]);
        Node Five = new Node("5", 1, 1, puzzle[1][0], puzzle[1][2], puzzle[0][1], puzzle[2][1]);
        Node Six = new Node("6", 2, 1, puzzle[2][0], puzzle[2][2], puzzle[1][1], null);

        Node Seven = new Node("7", 0, 2, puzzle[0][1], null, null, puzzle[1][2]);
        Node Eight = new Node("8", 1, 2, puzzle[1][1], null, puzzle[0][2], puzzle[2][2]);
        Node blank = new Node("blank", 2, 2, puzzle[2][1], null, puzzle[1][2], null);

    }

    public void swapAbove (Node[][] puzzle, Node blank, Node blankAbove) {

        if (blank.getLabel() == "blank" && blankAbove != null){

            Node temp = blank;
            Node temp2 = blankAbove;
            blank = temp2;
            blankAbove = temp;

            puzzle[temp.getX()][temp.getY()] = blankAbove;
            puzzle[temp2.getX()][temp.getY()] = blank;

        } else { 

            System.out.println("Invalid Move");

        }

    }

    //also have swapUp swapLeft and swapRight methods
}

Here is my Node class:

public class Node {

    String label;
    int x;
    int y;
    Node above;
    Node below;
    Node left;
    Node right;

    public Node(String label, int x, int y, Node above, Node below, Node left, Node right) {
            this.label = label;
            this.x = x;
            this.y = y;
            this.above = above;
            this.below = below;
            this.left = left;
            this.right = right;
    }

    //also have accessor methods for my variables

}
  • The heuristic for NullPointerExceptions is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Nov 24 '16 at 13:27
  • Also, don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Nov 24 '16 at 13:27
  • So not `if (blank.getLabel() == "blank" && blankAbove != null){`, but rather `if (blank.getLabel().equals("blank") && blankAbove != null){` – Hovercraft Full Of Eels Nov 24 '16 at 13:35
  • Thanks for your help, I will look deeper into the exception – user546482872 Nov 24 '16 at 13:37
  • 1
    Looks like you've created your puzzle 2D array, but are trying to use it before you fill it with objects. It's like trying to use eggs from an empty egg carton -- it won't work. First fill your array with objects *before* trying to use non-existing objects. – Hovercraft Full Of Eels Nov 24 '16 at 13:44
  • 1
    For instance, please see [this question and answers](http://stackoverflow.com/questions/3368156/want-a-multidimensional-array-but-get-a-null-pointer-exception) – Hovercraft Full Of Eels Nov 24 '16 at 13:51

0 Answers0