我正在使用slick2D来创建一个简单的Java游戏。不幸的是,我的java技能比简单简单。这个游戏将是一个记忆游戏,因为我觉得它很简单。我遇到的一个问题是我有一个NormalMode类和一个Square类。我在普通模式类中创建了一个Square对象来调用Square中的方法。所以,我试着调用一个以“图形g”作为边界的方法,结果我得到一个空指针异常错误(或者其他我不知道的东西)。
方形类:(此类中的最后一个方法目前尚未完成)
package data.src;
import java.util.Random;
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;
public Square() {
}
public boolean checkCorrect(){
return correct;
}
public boolean checkClickable(){
return clickable;
}
public boolean checkClicked(){
return clicked;
}
public void squareStart(int x ,int y, Graphics g){
if(correct == true){
g.drawImage(squareCorrect, x, y);
}
else if(correct == false){
g.drawImage(squareIncorrect, x, y);
}
g.drawString("Remember the blue squares!", x, y);
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
started = true;
while(started == true){
try {
squareCorrect = new Image("res/squareCorrect");
} catch (SlickException e) {
e.printStackTrace();
}
}
}
public void createRandom(){
Random rand = new Random();
int sRand = rand.nextInt(1);
if(sRand == 0){
correct = false;
clickable = true;
clicked = false;
}
else if(sRand == 1){
correct = true;
clickable = true;
clicked = false;
squares++;
}
}
public void youWin(int x, int y, Graphics g){
if(squares == 0){
g.drawString("You win!", x, y);
game.enterState(0);
}
}
}
普通模式课程:
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];
public NormalMode() {
}
@Override
public void init(GameContainer arg0, StateBasedGame Game)throws SlickException {
for(int i = 0; i < square.length; i++){
for(int j = 0; j < square.length; j++){
square[i][j] = new Square();
square[i][j].createRandom();
}
}
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException{
square[1][1].squareStart(40, 300, g);
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
}
@Override
public int getID() {
return 3;
}
}
P.S:如果我的代码看起来很愚蠢,请原谅我的完全废话。
错误:
Sun Jan 05 01:17:25 MST 2014 ERROR:null
java.lang.NullPointerException
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1384)
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1433)
at data.src.Square.squareStart(Square.java:46)
at data.src.NormalMode.render(NormalMode.java:32)
at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
at org.newdawn.slick.ScalableGame.render(ScalableGame.java:158)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at data.src.Core.main(Core.java:30)
Sun Jan 05 01:17:25 MST 2014 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at data.src.Core.main(Core.java:30)
答案 0 :(得分:1)
squareCorrect
和squareIncorrect
为空,因为您在使用变量之前未设置变量。
您应该在声明它们之后加载它们:
public Image squareIncorrect;
public Image squareCorrect;
{
squareIncorrect = new Image("res/squareCorrect");
squareIncorrect = new Image("res/squareIncorrect");
}