Android Question Fatal Exception: java.lang.RuntimeException: null object reference

PierPaduan

Active Member
Licensed User
Longtime User
Hi everybody.
Crashlytics gave me the following error. It has happened only 3 times on a Nokia 3.1 with Android 9.
Fatal Exception: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.objects.ServiceHelper it.ritmo.setandgo.service1._service' on a null object reference
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:233)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at it.ritmo.setandgo.service1.handleStart(service1.java:100)
at it.ritmo.setandgo.service1.access$000(service1.java:8)
at it.ritmo.setandgo.service1$2.run(service1.java:80)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6715)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

Caused by java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.objects.ServiceHelper it.ritmo.setandgo.service1._service' on a null object reference
at it.ritmo.setandgo.service1._service_start(service1.java:229)
at java.lang.reflect.Method.invoke(Method.java)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at it.ritmo.setandgo.service1.handleStart(service1.java:100)
at it.ritmo.setandgo.service1.access$000(service1.java:8)
at it.ritmo.setandgo.service1$2.run(service1.java:80)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6715)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

The line "service1.java:229" in service1.java should be the following:
B4X:
mostCurrent._service.StartForeground((int) (1),(android.app.Notification)(_vvvvvvvvvvvvvvvvvvvvvvvvvvv0.getObject()));
And it should correspond to the StartForeground line below:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim nNotify As Notification 
End Sub

Sub Service_Create
    nNotify.Initialize
    nNotify.Icon = ""      
    nNotify.Sound=False
    nNotify.Vibrate=False
    nNotify.Light=False
    nNotify.OnGoingEvent=True
    nNotify.SetInfo("", "", "")    
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(1,nNotify)
    '.......
Am I doing something wrong?
Can someone give to me an advice?
Thanks a Lot.
 

PierPaduan

Active Member
Licensed User
Longtime User
What is causing the service to start?
In this app there is a "Timer" activity that shows a countdown to the user.
If the user put the activity in background, the service starts every second and show to the user a notification with the remaining time.

The service starts from:
  • the "Timer" Activity_Create (but really I don't remember why I decided this years ago, I tried now removing this ServiceStart and all works anyway),
  • the "Timer" Activity_Pause,
  • the Service_Start with a StartServiceAt(... + 1 second ...)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Android will not allow you to restart a service in this interval.
Set the interval to restart the serrvice after 15 MINUTES or more.

Alternatively use a Foreground service and a timer.
 
Upvote 0
Top