空指针异常错误,我们再次见面

时间:2014-01-05 21:05:18

标签: java graphics nullpointerexception drawing slick2d

我最终决定将实际游戏部分用于我的记忆游戏,并再次获得空指针异常错误。我之前已经在这里问了同样的问题,所以如果这被视为垃圾邮件我很抱歉,但我无法弄清问题是什么!

是的我是编程的新手,所以对你为什么要做“在这里插入答案”的一点解释将是一个很大的帮助!

现在这是我的Square课程(这些是你必须记住的方格和游戏的起点):

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();
        }
    }
}

这是我的NormalMode类(我从square调用方法并渲染事物):

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;
    }
}

我也在使用精彩的Slick2D库!如果只有更好的文档。

错误:

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)

3 个答案:

答案 0 :(得分:7)

您永远不会在gameStarter中初始化NormalMode,因此当您尝试在gameStarter.startGame(g);中调用NormalMode#init()时,您尝试在空对象上执行方法。

实际上,您有很多地方未能初始化字段:

    课程game中的
  • Square(在game.wait(5000);中呼叫Square#startGame()
  • 课程g中的
  • NormalMode(传递给使用它的Square#startGame()

答案 1 :(得分:5)

您需要将gameStarter设置为某些内容。目前,您将其保留为null。

答案 2 :(得分:0)

在我的记事本应用程序中,发生了这个异常,因为我指的是一个未初始化的变量...所以你需要初始化gamestarter ...希望它有效:)