i am doing some android work using scala and stumbled into the following exception while using actor:
09-27 12:19:51.046: ERROR/AndroidRuntime(354): Uncaught handler: thread Thread-13 exiting due to uncaught exception
09-27 12:19:51.056: ERROR/AndroidRuntime(354): java.lang.NullPointerException
09-27 12:19:51.056: ERROR/AndroidRuntime(354): at scala.actors.TimerThread$$$$anon$$1.run(TimerThread.scala:44)
09-27 12:19:51.056: ERROR/AndroidRuntime(354): at java.lang.Thread.run(Thread.java:935)
after looking at the source (TimerThread.scala), i discovered a problem: timerThread might not be initialized before it is used in timerTask, thus the NPE. ("timerThread.synchronized {" is where the NPE is thrown) the fix should be simple enough, replace 2 occurences where "timerThread" appears in "timerTask" by "java.lang.Thread.currentThread" should give a semantically equivalent code without the risk of NPE.
in TimerThread.scala:
private val timerTask = new Runnable {
override def run = {
try {
while(true) {
timerThread.synchronized {
try {
val sleepTime = dequeueLateAndGetSleepTime
if (lateList.isEmpty) timerThread.wait(sleepTime)
} catch {
case t: Throwable => { throw t }
}
}
// process guys waiting for signal and empty list
for (wa <- lateList) {
if (wa.valid) {
wa.actor ! TIMEOUT
}
}
lateList = Nil
}
} catch {
case consumed: InterruptedException =>
// allow thread to quit
}
}
}
private var timerThread: Thread = {
val t = new Thread(timerTask)
t.start()
t
}
i am doing some android work using scala and stumbled into the following exception while using actor:
09-27 12:19:51.046: ERROR/AndroidRuntime(354): Uncaught handler: thread Thread-13 exiting due to uncaught exception 09-27 12:19:51.056: ERROR/AndroidRuntime(354): java.lang.NullPointerException 09-27 12:19:51.056: ERROR/AndroidRuntime(354): at scala.actors.TimerThread$$$$anon$$1.run(TimerThread.scala:44) 09-27 12:19:51.056: ERROR/AndroidRuntime(354): at java.lang.Thread.run(Thread.java:935)
after looking at the source (TimerThread.scala), i discovered a problem: timerThread might not be initialized before it is used in timerTask, thus the NPE. ("timerThread.synchronized {" is where the NPE is thrown) the fix should be simple enough, replace 2 occurences where "timerThread" appears in "timerTask" by "java.lang.Thread.currentThread" should give a semantically equivalent code without the risk of NPE.
in TimerThread.scala:
private val timerTask = new Runnable { override def run = { try { while(true) { timerThread.synchronized { try { val sleepTime = dequeueLateAndGetSleepTime if (lateList.isEmpty) timerThread.wait(sleepTime) } catch { case t: Throwable => { throw t } } }
}
private var timerThread: Thread = { val t = new Thread(timerTask) t.start() t }
regards,
walter