Android Question Attempt to read from field ... on a null object reference

LuigiTasca

Member
Licensed User
Longtime User
Hello,
A user reported to me this error that I can't replicate. This error occours only to this customer, nobody else has this problem:

B4X:
(NullPointerException) java.lang.NullPointerException: Attempt to read from field 'com.brand.appname.main com.brand.appname.rooms._main' on a null object reference
java.lang.NullPointerException: Attempt to read from field 'com.brand.appname.main com.brand.appname.rooms._main' on a null object reference
    at com.brand.appname.rooms._activity_resume(rooms.java:845)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at com.brand.appname.rooms$ResumeMessage.run(rooms.java:317)
    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:6702)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

The user works mostly with the "rooms" activity and with a third activity. The error occours when the user is using the third activity and the user navigate to the "rooms" activity. As you can see the error happens in the "activity_resume" sub.

If I check in b4a the error line, it seems that it crashes when the app tries to read a global variable from the "main" activity. All the global variables in my project are declared in the Process_Globals. Obviously the variables can't normally be null, these ones are booleans or long.

If it can help, I know that these users frequently put the app in stand-by while it's doing something (like switching to "rooms" activity).

Tell me if I can give you more informations,
Thank you
 

William Lancee

Well-Known Member
Licensed User
Longtime User
#8 and #9 in that thread suggest that there might be timing problem when the activity is paused and resumed.
Since you can't replicate the problem, we have little else to go on. Is there an Activity_WindowFocusChanged in your code?

@Spavlyuk explained:
Activity_WindowFocusChanged is somewhat of a special event. If you investigate the generated main.java code, you'll find that when the application is paused, mostCurrent is set to null. Activity_WindowFocusChanged can be called even if the application is paused, meaning you cannot access any of your variables during that time.
 
Upvote 0

LuigiTasca

Member
Licensed User
Longtime User
Little update if it can help: watching my logs, it seems that it is called "activity_pause" (on "rooms") immediately after "activity_resume" (on "rooms" too) is called. Probably, the user locks the handheld screen immediately after the app lead him to the "rooms" activity. (It seems that the app crashes while it is processing "Activity_resume" and the user locks the screen) Anyway, I can't replicate this, even trying to do the same thing.

Anyway I can't understand how this can generate that error. (main null etc...)

#8 and #9 in that thread suggest that there might be timing problem when the activity is paused and resumed.
Since you can't replicate the problem, we have little else to go on. Is there an Activity_WindowFocusChanged in your code?

@Spavlyuk explained:
No, I haven't "Activity_WindowFocusChanged" in this project.
 
Upvote 0
Top