MrPork
MrPork

Reputation: 75

Null pointer exception error, we meet again

I finally decided to make the actual game part for my memory game, and I get the null pointer exception error, once again. I've already asked the same question on here before, so I'm sorry if this counts as spam, but I cannot figure what the problem is at all!

Yes I'm new to programming, so a little explanation of why you have to do "Insert answer here" would be a great help!

Now here's my Square class (these are the squares you have to remember and where the game is started from):

package data.src;

import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Square {

    private StateBasedGame game;
    boolean correct;
    boolean clickable;
    boolean clicked;
    boolean started;
    int squares;
    int squareX;
    int squareY;
    public Image squareIncorrect;
    public Image squareCorrect;

    //For drawing these images from other classes
    {
        try {
            squareIncorrect = new Image("res/squareIncorrect.png");
        } catch (SlickException e1) {

            e1.printStackTrace();
        }
        try {
            squareCorrect =  new Image("res/squareCorrect.png");
        }
        catch (SlickException e) {

            e.printStackTrace();
        }
    }

    public Square(GameContainer container) {
                container.getGraphics();

    }

    public boolean checkCorrect(){
        return correct;

    }

    public boolean checkClickable(){
        return clickable;
    }

    public boolean checkClicked(){
        return clicked;

    }

    public int returnNumberOfSquares(){
        return squares;

    }

    //Draw appropriate square on screen depending on the creatRandom. CREATE THE RANDOM FIRST!
    public void squareDraw(int x ,int y, Graphics g)throws SlickException{

        if(correct == true){
            g.drawImage(squareCorrect, x, y);

        }
        else if(correct == false){
            g.drawImage(squareIncorrect, x, y);

        }
    }

    public void createRandom() {

        Random rand = new Random();
        int sRand = rand.nextInt(2);

        if(sRand == 0) {
            correct = false;
            clickable = true;
            clicked = false;
            System.out.println("This square is a phony!");

        }
        else if (sRand == 1) {
            correct = true;
            clickable = true;
            clicked = false;
            System.out.println("This is a true Square!");
            squares = squares+1;
        }
    }

    public void youWin(int x, int y, Graphics g){
        if (squares == 0) {
            g.drawString("You win!", x, y);
            try {
                wait(4000);
                game.enterState(0);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //Normal mode Methods begin here. Ends at EndGame().
    public void  startGame(Graphics g) throws SlickException {

        g.setColor(Color.red);
        g.drawString("Remember the squares! The game starts in 5 seconds!", 300, 200);
        clickable = false;
        started = false;
        try {
            game.wait(5000);
            started = true;
            clickable = true;
            returnNumberOfSquares();
            squareCorrect = new Image("res/squareIncorrect");
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

And here is my NormalMode Class (where I call methods from square and render things):

package data.src;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class NormalMode extends BasicGameState {

    private Square[][] square = new Square[4][4];
    private Square gameStarter;
    private Graphics g;

    public NormalMode() {

    }

    @Override
    public void init(GameContainer arg0, StateBasedGame Game)throws SlickException {
        gameStarter = new Square(arg0);
        gameStarter.startGame(g);
        for (int i = 0; i < square.length; i++){
            for(int j = 0; j < square.length; j++){
                square[i][j] = new Square(arg0);
                square[i][j].createRandom();

            }
        }
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g)
           throws SlickException {

        square[0][0].squareDraw(100, 300, g);
        square[0][1].squareDraw(150, 300, g);
        square[0][2].squareDraw(200, 300, g);
        square[0][3].squareDraw(250, 300, g);

        square[1][0].squareDraw(100, 400, g);
        square[1][1].squareDraw(150, 400, g);
        square[1][2].squareDraw(200, 400, g);
        square[1][3].squareDraw(250, 400, g);

        square[2][0].squareDraw(100, 500, g);
        square[2][1].squareDraw(150, 500, g);
        square[2][2].squareDraw(200, 500, g);
        square[2][3].squareDraw(250, 500, g);

        square[3][0].squareDraw(100, 600, g);
        square[3][1].squareDraw(150, 600, g);
        square[3][2].squareDraw(200, 600, g);
        square[3][3].squareDraw(250, 600, g);
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta)
            throws SlickException {

    }

    @Override
    public int getID() {
        return 3;
    }
}

I am using the wonderful Slick2D library as well! If only it had better documentation.

The error:

Exception in thread "main" java.lang.NullPointerException
    at data.src.NormalMode.init(NormalMode.java:22)
    at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
    at org.newdawn.slick.ScalableGame.init(ScalableGame.java:69)
    at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
    at data.src.Core.main(Core.java:30)

Upvotes: 0

Views: 265

Answers (3)

raman rayat
raman rayat

Reputation: 414

In my notepad application, this exception was happening because I was pointing toward a variable which was not initialized... So you need to initialize gamestarter... Hopefully it works :)

Upvotes: 0

Mark Elliot
Mark Elliot

Reputation: 77044

You never initialize gameStarter in NormalMode, so when you attempt to call gameStarter.startGame(g); in NormalMode#init() you're trying to execute a method on a null object.

Actually, you have a variety of places you've failed to initialize fields:

  • game in class Square (calling game.wait(5000); in Square#startGame())
  • g in class NormalMode (passed to Square#startGame() where it's used)

Upvotes: 7

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

You need to set gameStarter to something. Currently, you're leaving it as null.

Upvotes: 5

Related Questions