Hi!
I run my game in J2ME Wireless ToolKit 2.2 successfully, but when I run my game in Nokia Mobile Internet Toolkit or a real nokia phone that had a error of Null Pointer Exception. The error was happened when some action changes in the game or exiting the game. Here is my program:
SpeedMIDlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class SpeedMIDlet extends MIDlet implements CommandListener {
private HCanvas canvas;
protected Image background;
public void startApp() {
if (canvas == null) {
canvas = new HCanvas(Display.getDisplay(this));
Command exitCommand = new Command("Exit", Command.EXIT, 0);
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
}
// Start up the canvas
canvas.start();
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {
canvas.stop();
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
destroyApp(true);
notifyDestroyed();
}
}
}
Some parts of HCanvas.java
public HCanvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 33;
// Clear the input delay
inputDelay = 0;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Initialize the random number generator
rand = new Random();
// Initialize the game variables
gameOver = false;
numLives = 3;
score = 0;
// Initialize the background image and chicken and car sprites
try {
background = Image.createImage("/Highway.png");
chickenHead = Image.createImage("/ChickenHead.png");
speed = Image.createImage("/speed.png");
blackholeSprite[0] = new Sprite(Image.createImage("/Blackhole.png"));
blackholeSprite[0].setPosition(70, 77);
blackholeSprite[1] = new Sprite(Image.createImage("/Blackhole.png"));
blackholeSprite[1].setPosition(90, 107);
blackholeSprite[2] = new Sprite(Image.createImage("/Blackhole.png"));
blackholeSprite[2].setPosition(121, 60);
blackholeSprite[3] = new Sprite(Image.createImage("/Blackhole.png"));
blackholeSprite[3].setPosition(100, 20);
chickenSprite = new Sprite(Image.createImage("/Person.png"), 20, 26);
chickenSprite.setPosition(2, 77);
carSprite[0] = new Sprite(Image.createImage("/Car1.png"));
carSprite[0].setPosition(40, 0);
carYSpeed[0] = 4;
carSprite[1] = new Sprite(Image.createImage("/Car3.png"));
carSprite[1].setPosition(111, 67);
carYSpeed[1] = -4;
}
catch (IOException e) {
System.err.println("Failed loading images!");
e.printStackTrace();
}
// Initialize the music and wave players
try {
InputStream is = getClass().getResourceAsStream("Music.mid");
musicPlayer = Manager.createPlayer(is, "audio/midi");
musicPlayer.prefetch();
is = getClass().getResourceAsStream("Celebrate.wav");
celebratePlayer = Manager.createPlayer(is, "audio/X-wav");
celebratePlayer.prefetch();
is = getClass().getResourceAsStream("Squish.wav");
squishPlayer = Manager.createPlayer(is, "audio/X-wav");
squishPlayer.prefetch();
is = getClass().getResourceAsStream("GameOver.wav");
gameoverPlayer = Manager.createPlayer(is, "audio/X-wav");
gameoverPlayer.prefetch();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (MediaException me) {
me.printStackTrace();
}
// Start playing the music indefinitely
try {
musicPlayer.setLoopCount(-1);
musicPlayer.start();
}
catch (MediaException me) {
me.printStackTrace();
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Close the music and wave players
musicPlayer.close();
celebratePlayer.close();
squishPlayer.close();
gameoverPlayer.close();
// Stop the animation
sleeping = true;
}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
update();
draw(g);
try {
Thread.sleep(frameDelay);
}
catch (InterruptedException ie) {ie.printStackTrace();}
}
}
private void update() {
// Check to see whether the game is being restarted
if (gameOver) {
int keyState = getKeyStates();
if ((keyState & FIRE_PRESSED) != 0) {
// Start a new game
try {
musicPlayer.setMediaTime(0);
musicPlayer.start();
}
catch (MediaException me) {
me.printStackTrace();
}
chickenSprite.setPosition(2, 77);
gameOver = false;
score = 0;
numLives = 3;
}
// The game is over, so don't update anything
return;
}
// Process user input to move the chicken
if (++inputDelay > 2) {
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
chickenSprite.move(-6, 0);
chickenSprite.nextFrame();
}
else if ((keyState & RIGHT_PRESSED) != 0) {
chickenSprite.move(6, 0);
chickenSprite.nextFrame();
}
if ((keyState & UP_PRESSED) != 0) {
chickenSprite.move(0, -6);
chickenSprite.nextFrame();
}
else if ((keyState & DOWN_PRESSED) != 0) {
chickenSprite.move(0, 6);
chickenSprite.nextFrame();
}
checkBounds(chickenSprite, false);
// Reset the input delay
inputDelay = 0;
}
// See whether the chicken made it across
if (chickenSprite.getX() > 154) {
// Play a sound for making it safely across
try {
celebratePlayer.start();
}
catch (MediaException me) {
me.printStackTrace();
}
// Reset the chicken position and increment the score
chickenSprite.setPosition(2, 77);
score += 25;
}
// Update the car sprites
for (int i = 0; i < 2; i++) {
// Move the car sprites
carSprite[i].move(0, carYSpeed[i]);
checkBounds(carSprite[i], true);
for (int j = 0; j < 4; j++) {
// Move the blackhole sprites
blackholeSprite[j].move(0, 0);
checkBounds(blackholeSprite[j], true);
// Check for a collision between the chicken and cars
if ((chickenSprite.collidesWith(carSprite[i], true))||(chickenSprite.collidesWith(blackholeSprite[j], true))) {
// Play a sound for losing a chicken
try {
squishPlayer.start();
}
catch (MediaException me) {
me.printStackTrace();
}
// Check for a game over
if (--numLives == 0) {
// Stop the music and play a game over sound
try {
musicPlayer.stop();
gameoverPlayer.start();
}
catch (MediaException me) {
me.printStackTrace();
}
gameOver = true;
} else {
// Reset the chicken position
chickenSprite.setPosition(2, 77);
}
// No need to continue updating the car sprites
break;
}
}
}
}
Could anyone help me? Thx!

Reply With Quote



