Android Question how to debug this? Sleep(0) vs DoEvents


Licensed User
Longtime User
HI, All

App user informed me that the latest version is stopped after some time.
After some test i have found that after 360 seconds ALWAYS app is stopped with such error, even in the Debug mode:

Starter.tim_Tick: 357
Starter.tim_Tick: 358
Starter.tim_Tick: 360
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean anywheresoftware.b4a.BA.isActivityPaused()' on a null object reference
at anywheresoftware.b4a.objects.Timer$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loopOnce(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

It seems to me that it is some timer tick sub and isPaused function. But i cannot find such function usage in all timers subs.
App is old, and i found that DOEVENTS is still used, but not all places, some are already with Sleep(0) instead.

And when i placed DOEVENTS everywere instead of Sleep(0) .... hanging is over, app works fully OK.
When to use Sleep(0) - it's again error.

How to understand it ?


Licensed User
Longtime User
The error is occurring when the Timer is trying to invoke your Timer Sub. It seems to be finding that the ba object is null.
    public void run() {
      Timer parentTimer = this.parent.timer;
      if (parentTimer == null || this.currentTimer != parentTimer.relevantTimer)
      BA.handler.postDelayed(this, parentTimer.interval);
      if (! && !Msgbox.msgboxIsVisible()) // <--------------This is line 104, false, parentTimer.eventName, true, new Object[0]);
Is your Timer in the Starter service, it looks like it, or in another Service? As it is an old app it looks like some behaviour in Android has changed in later versions.

DoEvents and Sleep behave differently. DoEvents executes some of the messages on the app message loop without returning to the message loop then continues in your Sub. Sleep actually returns to the app message loop and then your Sub is re-entered some time later. Because DoEvents executes the messages itself it has become very fragile over the years as the message loop code has become more complicated and is now deprecated.
Upvote 1


Licensed User
Longtime User
or in another Service
Yes, timer in a separate foreground service with a notification. All the time it was tested under Android 12.
How to be here ?
Last edited:
Upvote 0