Android Question Check is null

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi all!
I have a strange issue.. why this code got me an error?

B4X:
dim ReloadNotif as Notifica = null
lbTime.Text = Utils.IIF(ReloadNotif <> Null,ReloadNotif.custom.Get("time"),"120")

public Sub IIF(c As Boolean, TrueRes As Object, FalseRes As Object) As Object
    If c Then
        Return TrueRes
     Else
          Return FalseRes
    End If
End Sub

B4X:
*** Service (starter) Create ***
Found 22 strings.
loc it
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (addwifinotification) Create, isFirst = true **
** Activity (addwifinotification) Resume **
addwifinotification_loadactivity (B4A line: 99)
lbTime.Text = Utils.IIF(ReloadNotif.custom <> Nul
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.objects.collections.Map b4a.example.utils$_notifica.custom' on a null object reference
    at b4a.example.addwifinotification._loadactivity(addwifinotification.java:788)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1151)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)


If i use this works..
B4X:
If (ReloadNotif = Null) Then lbTime.Text = "120" Else lbTime.Text = ReloadNotif.custom.Get("time")
    lbTime.Text = lbTime.Text & " "&Utils.loc.Localize("Utils4")
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Because when you use your IIF method, all 3 parameters have to be passed to the method. Your second parameter is ReloadNotif.custom.Get("time") and since ReloadNotif is null, the access to the custom field fails (as the error message shows). In the second example, the ReloadNotif.custom.Get("time") is not accessed until after the check for null is done. Hope it makes sense.
 
Upvote 0
Top