A problem with callSerially()

Hi, guys,

I modifed a sample sweeper anmimation MIDlet from Chapter 13 of the book <<Beginning J2ME From Novice to Professional>>. The original sweeper used a separate animaton thread along with the UI thread. I managed somehow to modify the code to use the Display.callSeriall() method and get rid of the additional thread. The code compile with Wireless Toolkit 2.5.1, but when I start to run the MIDlet, the emulator just signals some runtime errors, mainly "java.lang.NullPointerException". I thinked very hard about it, but just desparately can't figure out what is wrong. Could anyone please help me and tell me what's wrong with my code, any helpful information is greatly appreciated.

The following is my code listings.

////////////////FastSweep.java////////////////////////

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

public class FastSweep extends MIDlet {

public void startApp() {

final FastSweepCanvas sweeper = new FastSweepCanvas();

sweeper.start();

sweeper.addCommand(new Command("Exit", Command.EXIT, 0));

sweeper.setCommandListener(new CommandListener() {

public void commandAction(Command c, Displayable s) {

sweeper.stop();

notifyDestroyed();

}

});

Display.getDisplay(this).setCurrent(sweeper);

}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

}

//////////////////End of FastSweep.java/////////////////////

////////////////////FastSweepCanvas.java/////////////////////

import javax.microedition.lcdui.*;

public class FastSweepCanvas extends Canvas implements Runnable {

private Display mDisplay;

private boolean mTrucking;

private int mTheta;

private int mBorder;

private int mDelay;

public FastSweepCanvas() {

mTheta = 0;

mBorder = 10;

mDelay = 50;

}

public void start() {

mTrucking = true;

run();

}

public void stop() {

mTrucking = false;

}

public void paint(Graphics g) {

int width = getWidth();

int height = getHeight();

// Clear the Canvas.

g.setGrayScale(255);

g.fillRect(0, 0, width - 1, height - 1);

int x = mBorder;

int y = mBorder;

int w = width - mBorder * 2;

int h = height - mBorder * 2;

for (int i = 0; i < 8; i++) {

g.setGrayScale((8 - i) * 32 - 16);

g.fillArc(x, y, w, h, mTheta + i * 10, 10);

g.fillArc(x, y, w, h, (mTheta + 180) % 360 + i * 10, 10);

}

}

public void run() {

while (mTrucking) {

mTheta = (mTheta + 1) % 360;

repaint();

mDisplay.callSerially(this);

}

}

}

////////////////////End of FastSweepCanvas.java/////////////////////

and here is the complete runtime errors:

startApp threw an Exception

java.lang.NullPointerException

java.lang.NullPointerException

at FastSweepCanvas.run(+30)

at FastSweepCanvas.start(+9)

at FastSweep.startApp(+12)

at javax.microedition.midlet.MIDletProxy.startApp(+7)

at com.sun.midp.midlet.Scheduler.schedule(+270)

at com.sun.midp.main.Main.runLocalClass(+28)

at com.sun.midp.main.Main.main(+80)

[3333 byte] By [SeaScapea] at [2007-11-27 10:50:31]
# 1

you never actually initialized "mDisplay" object, so when you're calling mDisplay.callSerially(this)... mDisplay is null. So, in FastSweepCanvas() constructor, you'll need to set that to the MIDlets display.

pandora_fooa at 2007-7-29 11:25:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Thank you pandora_foo! I will try it out later.

SeaScapea at 2007-7-29 11:25:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...