我最终决定为我的记忆游戏制作出真正的游戏零件,并且再次出现空指针异常错误。之前我已经在这里问过同样的问题,所以很抱歉这是否属于垃圾邮件,但是我根本无法弄清楚问题出在哪里!

是的,我是编程新手,所以对为什么您必须做“在此处插入答案”做一点解释将对您大有帮助!

现在这是我的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类(在这里我从正方形调用方法并渲染事物):

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)

最佳答案

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

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


game中的Square(在game.wait(5000);中调用Square#startGame()
g中的NormalMode(传递给使用它的Square#startGame()

10-06 06:06