Bug? [CLOSED]Null Pointer Exception When class method is run in library mode

cimperia

Active Member
Licensed User
Longtime User
OS: Windows7
.Net > 4.5
B4A: 5.20

I have created a few class methods that accept a JavaObject parameter. The parameter may be null or not.

If I run the app (emulator or device) with the Class module, everything runs OK.
If I compile the Class module to a library and run the app, an Exception is raised when the parameter is set to Null.

The test pLocale.IsInitialized fails in library mode. (Does this mean that the java code generated is different in library mode from class module mode?)

Is there another way to check if an argument/object is null that would work around this?

Please, bring us overloading or optional parameters.;)

Example of Class Method
B4X:
Public Sub GetDisplayName(pLocale As JavaObject) As String
   'Assign Class current's locale as Reflector's target
   r.Target = locale_

   If pLocale.IsInitialized Then
     Log("Initialized:True")
     Return r.RunMethod4("getDisplayName", Array As Object(pLocale), Array As String("java.util.Locale"))
   Else
     Log("Initialized:False")
     Return r.RunMethod4("getDisplayName", Array As Object(getDefault_), Array As String("java.util.Locale"))
   End If
End Sub

Calls to the subs:
B4X:
'Succeeds in both Class Module and Library calls
Log(ahl.GetDisplayName(ahl.Locale))

'Fails in library call, succeeds in Class call.
Log(ahl.GetDisplayName(Null))

Exception Message
B4X:
balocale_getdisplayname (java line: 343)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean anywheresoftware.b4j.object.JavaObject.IsInitialized()' on a null object reference
   at b4a.example.balocale._getdisplayname(balocale.java:343)
   at b4a.example.main._activity_create(main.java:334)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
   at b4a.example.main.afterFirstLayout(main.java:102)
   at b4a.example.main.access$000(main.java:17)
   at b4a.example.main$WaitForLayout.run(main.java:80)
   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:5257)
   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:903)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean anywheresoftware.b4j.object.JavaObject.IsInitialized()' on a null object reference
 
Last edited:

cimperia

Active Member
Licensed User
Longtime User
It worked. Thanks.

Still worrying that it would work in Class mode though.
 
Last edited:
Top