gpt4 book ai didi

java - 空指针异常错误,我们又见面了

转载 作者:行者123 更新时间:2023-12-01 06:35:03 25 4
gpt4 key购买 nike

我最终决定为我的内存游戏制作实际的游戏部分,然后我再次收到空指针异常错误。我之前已经在这里问过同样的问题,所以如果这被视为垃圾邮件,我很抱歉,但我根本不知道问题出在哪里!

是的,我是编程新手,所以对为什么必须执行“在此处插入答案”进行一些解释将会有很大帮助!

现在这是我的 Square 类(这些是您必须记住的方 block 以及游戏从哪里开始):

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)

最佳答案

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

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

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

关于java - 空指针异常错误,我们又见面了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20939151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com