i am using slick2D to create a simple java game. Unfortunately my java skills are simpler than simple. This game will be a memory game, since i thought it would be simple to make. One of the problems i have is that i have a NormalMode class, and a Square class. I created a Square object in my normal mode class to call methods from Square. So, i tried to call a method of it that had "graphics g" as a perimeter, and as a result i get a null pointer exception error (Or something else i'm unaware of).
Square Class: (The last method in this class is unfinished at the moment)
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);
}
}
}
Normal Mode Class:
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: Excuse my complete nonsense of stuff if my code seems stupid.
The error:
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)
squareCorrect
and squareIncorrect
are null because you don't set the variables before using them.
You should load them just after declaring them:
public Image squareIncorrect;
public Image squareCorrect;
{
squareIncorrect = new Image("res/squareCorrect");
squareIncorrect = new Image("res/squareIncorrect");
}