Android Question Crashes only when debugging

Lee Gillie CCP

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Log("CheckOut.Activity_Pause")
    Globals.SaveSetting(Globals.SETTING_TRUCK_NAME,acetTruck.Text)
    Globals.SaveSetting(Globals.SETTING_DRIVER_NAME,etDriver.Text)
    Globals.SaveSetting(Globals.SETTING_TRUCK_ODOMETER,etOdometer.Text)
End Sub

The first Globals.SaveSetting is line 63. I get this exception at line 63...
CheckOut.Activity_Pause
Error occurred on line: 63 (CheckOut)
java.lang.NullPointerException: Attempt to read from field 'odp.eljaydelivery.globals odp.eljaydelivery.checkout._globals' on a null object reference
at odp.eljaydelivery.checkout._activity_pause(checkout.java:445)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.ShellBA$2.run(ShellBA.java:115)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

Settings in module globals is about saving named values with a map, and reading and writing a file with these settings.

Guessing this may be a debugger problem. But it is not cool with this crashing all the time I am testing. Is there ANYTHING I can do to avoid the crashes?
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I made the change, but do not observe any variance. It continues to crash only in debug mode.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
The project is getting large, so perhaps I can give a better hint at what the SaveSettings routine is all about?

As discussed I renamed the Globals module. So the PAUSE routine is now coded as:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Log("CheckOut.Activity_Pause")
    GlobalModule.SaveSetting(GlobalModule.SETTING_TRUCK_NAME,acetTruck.Text)
    GlobalModule.SaveSetting(GlobalModule.SETTING_DRIVER_NAME,etDriver.Text)
    GlobalModule.SaveSetting(GlobalModule.SETTING_TRUCK_ODOMETER,etOdometer.Text)
End Sub

Then in GlobalModule, SaveSetting is
B4X:
Public Sub SaveSetting( name As String, value As String )
    LoadOrInitializeSettings
    Settings.Put(name,value)
    File.WriteMap(SettingsPath,SettingsFilename,Settings)
End Sub

and
B4X:
Private Sub LoadOrInitializeSettings
    If Not( Settings.IsInitialized ) Then
        If File.Exists(SettingsPath,SettingsFilename) Then
             Settings = File.ReadMap(SettingsPath,SettingsFilename)
        Else
            Settings.Initialize
        End If
    End If
End Sub

And "Settings" is merely a MAP in Process_Globals of the GlobalModule module:
B4X:
    Private Settings As Map
    Private SettingsPath As String = File.DirInternal
    Private SettingsFilename As String = "settings.properties"

Another part is the names of setttings, which are also in Process_Globals of GlobalModule:
B4X:
    Public SETTING_DRIVER_NAME As String = "SETTING_DRIVER_NAME"
    Public SETTING_TRUCK_NAME As String = "SETTING_TRUCK_NAME"
    Public SETTING_TRUCK_ODOMETER As String = "SETTING_TRUCK_ODOMETER"
    Public LAST_MILEAGE_TRUCK As String = "LAST_MILEAGE_TRUCK"
    Public LAST_MILEAGE_ODOMETER As String = "LAST_MILEAGE_ODOMETER"
    Public CHECKIN_STATE As String = "CHECKIN_STATE"
    Public SETTING_SERVICE_HOST As String = "SETTING_SERVICE_HOST"
    Public PRODUCTS_LATESTUPLOAD As String = "Products_LatestUpload"

And finally, with renaming module Globals, the error indicator is slightly different than originally reported. It is now
CheckOut.Activity_Pause
Error occurred on line: 59 (CheckOut)
java.lang.NullPointerException: Attempt to read from field 'odp.eljaydelivery.globalmodule odp.eljaydelivery.checkout._globalmodule' on a null object reference
at odp.eljaydelivery.checkout._activity_pause(checkout.java:445)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.ShellBA$2.run(ShellBA.java:115)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

Again, when compiled RELEASE this seems to run fine, but in debug, this exception is thrown consistently.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Also to note, the PAUSE is of an Activity on the stack. The main activity starts an activity called CheckOut, and the PAUSE is for that CheckOut activity. After this the main activity resumes.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I dabble a bit in B4A and have one production app, but spend most of my time in MS Visual Studio working on Desktop applications, web sites, and Windows Services.

With Erel's help I resolved the issue stated at the top of this thread. But along the way I was reminded of some helpful strategies.

The B4A translator provides a number of warning messages when compiling. Things like naming classes same as activities, and various other things. I have NOT been able to change my code to eliminate ALL warnings, but I find that striving to minimize the number of these is helpful.

The ACTIVITY PAUSE event is a bugger to debug, because of Android imposed limitations. Can not set a break point here, or linger. Also, I find crash info seen in the LOG is not always the core cause of a problem. In general I find putting up a temporary button that basically will execute the logic in ACTIVITY PAUSE and THEN either finish the activity, or else start another activity. Caution to avoid executing this twice. But through this technique I was able to trace through a number of problems I was having in the ACTIVITY PAUSE. This is probably like "well... yeah, duh!" for those who live in B4A, but for any relative beginner, such as I consider myself, this approach can save you many hours trying to hunt down an elusive problem.
 
Upvote 0
Top