Exception in thread & ldquo; hand & rdquo; java.lang.NullPointerException (15-Puzzle)

advertisements

First of all, this question is about 15-Puzzle. And I'm trying to learning how to code it in Java language.

I have to study Artificial Intelligence at my College about IDA* algorithm, and found this link by Brian Borowski: http://brian-borowski.com/Software/Puzzle/ that was a pretty good link, but I've got an error when I'm trying to build it manually on my Eclipse.

Can anybody help me? I got an error that says:

Exception in thread "main" java.lang.NullPointerException
    at Puzzle.setNumOfTiles(Puzzle.java:45)
    at Puzzle.<init>(Puzzle.java:25)
    at GUI.<init>(GUI.java:196)
    at Application.<init>(Application.java:19)
    at Application.main(Application.java:27)

I already tried to trace the error, google searching, but still i cannot find the solution. It said that NullPointerException happen when some variable have a null value. Can anybody help me here?

Here are the source code

Application.java

public class Application {

    /**
     * Default constructor.
     * Creates an instance of the gui.
     */
    public Application() {
        ApplicationStarter appStarter = null;
        new GUI(appStarter); // Line 19
    }

    /**
     * Main method.
     * Creates an instance of the application.
     */
    public static void main(final String[] args) {
        new Application(); // Line 27
    }
}

Puzzle.java

public Puzzle(final int numOfTiles) {
    super();
    setNumOfTiles(numOfTiles); // Line 25
    setSize(panelWidth, panelHeight);
    setDoubleBuffered(true);
}

public void setNumOfTiles(final int numOfTiles) {
    this.numOfTiles = numOfTiles;
    dimension = (int)Math.sqrt(numOfTiles);
    imageArray = new Image[numOfTiles];
    final String file = "images/" + (numOfTiles - 1) + "-puzzle/shrksign_";
    imageArray[0] = null;
    for (int i = 1; i < numOfTiles; ++i) {
        final StringBuilder builder = new StringBuilder(file);
        if (i <= 9) {
            builder.append("0");
        }
        builder.append(Integer.toString(i));
        builder.append(".gif");
        imageArray[i] = ImagePanel.getImage(builder.toString());
    }
    imageWidth  = imageArray[1].getWidth(null); // Line 45
    imageHeight = imageArray[1].getHeight(null);
    panelWidth  = imageWidth * dimension;
    panelHeight = imageHeight * dimension;
    state = new byte[numOfTiles];
    sleepMs = 500 / imageWidth;
    animationDone = true;
    resetOrder();
}

GUI.java

...

this.puzzle = new Puzzle(PuzzleConfiguration.getNumOfTiles());

...


You are completely right about the meaning of the exception:

It said that NullPointerException happen when some variable have a null value.

What you need to do is look at the setNumOfTiles method, specifically line 45 (of the whole source file, not of the method) and see what happens there.

Most probably you are using an uninitialized variable OR passing one as an argument.

Good luck.