我刚才问了同样的问题,我被告知初始化我的squareChecker变量,所以我做了。由于某些我不知道的原因,我仍然会得到同样的错误。我整天都花了很多时间试图自己解决这个问题,但无济于事。有谁知道我的问题是什么?
我的普通模式类(我从方形类中调用方法并渲染事物):
public class NormalMode extends BasicGameState {
private Square[][] square = new Square[4][4];
private Square squareChecker;
Graphics g;
public NormalMode() { }
@Override
public void init(GameContainer arg0, StateBasedGame Game)throws SlickException {
squareChecker = new Square(arg0);
squareChecker.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;
}
}
我的广场班:
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) { }
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);
started = true;
}
else if(correct == false){
g.drawImage(squareIncorrect, x, y);
started = true;
}
}
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();
}
}
}
public void startGame(Graphics g){
g.drawString("Remember the blue squares! The game will start in 5 seconds!", 200, 300);
}
}
这是我得到的错误:
Exception in thread "main" java.lang.NullPointerException
at data.src.Square.startGame(Square.java:112)
at data.src.NormalMode.init(NormalMode.java:23)
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)
答案 0 :(得分:1)
在NormalMode.Init()
中,您拨打squareChecker.startGame(g);
g
定义为Graphics g;
。
startGame()
来电g.drawString()
。
这使我得出结论,在将g
传递给startGame()
方法之前,您不会将其{{1}}实例化。