Games Crashlytics: How to avoid a java.lang.NullPointerException?

Gunther

Active Member
Licensed User
Longtime User
Hello,

Firebase' Crashlytics reports very seldom a NullPointerException in the getWorldCenter of the X2 library.

The code in the Game_Tick Event is extremly unspectacular:
B4X:
...
Dim v As B2Vec2 = Ball1.Body.WorldCenter.CreateCopy
X2.UpdateWorldCenter(v)
...

How does one can avoid (= test before) if a bad vector v is given to that sub?
A simple "Try ... Catch ... End Try"-Structure will not help since the structure is basically correct.

Here the CrashReport:
B4X:
Caused by java.lang.NullPointerException
       at anywheresoftware.b4a.objects.B2Body.getWorldCenter(B2Body.java:46)
       at twinnypoints.gg.goilsoft.de.game._tick(game.java:3162)
       at twinnypoints.gg.goilsoft.de.x2utils$ResumableSub_MainLoop.resume(x2utils.java:856)
       at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1690)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5590)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
Not sure if this is valid enough in all cases?
B4X:
If  v <> Null Then X2.UpdateWorldCenter(v.CreateCopy)
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
I will give it a try with:
B4X:
If Ball1.Body.IsInitialized Then
    Dim v As B2Vec2 = Ball1.Body.WorldCenter.CreateCopy
    X2.UpdateWorldCenter(v)
Else
    Starter.CL.CLog("Something unexpected happened...")
End If
 

Gunther

Active Member
Licensed User
Longtime User
Well, up to now no additional crash reports due to this.

Only some 'Out of Memory'-Errors in the 'b4a.example.bitmapcreator._initialize' on older devices with Android 4.4.4 running.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…