Thursday, February 21, 2008

A small problem in programming with Java ME

Recently I feel weird by dealing with threads and alerts in programming with Java ME. One of the problems is that my program will occur NullPointException in the console.

java.lang.NullPointerException
at javax.microedition.lcdui.Alert$1.commandAction(Alert.java:663)
at javax.microedition.lcdui.Alert$timeoutTask.run(Alert.java:1110)
at java.util.TimerThread.mainLoop(+237)
at java.util.TimerThread.run(Timer.java:462)


Based on this Exception message, I checked the source code of Java SE 1.6 version ( Thanks for Sun Microsystems to open the Java source ) of Timer and TimerThread because I cannot see the source in Java ME, so I use JavaSE to guess the structure of JavaME. P.S: The core APIs are still different even the package name are the same. And also, I drew a diagram as follows for myself to figure out the whole situation about my threads, alert, and timertask. This problem I guess is in "Alert" class.

Alert class allows to setup the timing for showing on the screen during it appears, and it still has a TimerThread to control seconds. TimerThread only can do one task at the same time. So there is a situation: If I let my "AlertThread" setup the alert before the alert disappear ( done its work) and it happens more than 1 task needed to do. Then it will occur the problem. After I changed the timeout of the Alert, it didn't happen the problem again.


Using the emulator of Wireless Tool kit 2.5.2 to run TimerMIDlet:




1 comment:

monk said...

thanks for this post buddy!