Android Question Error occurs only in Release mode

geoffschultz

Member
Licensed User
Longtime User
If I compile the following code in Debug mode, this executes fine. If I compile it as Release, I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference

B4X:
If cust.ContainsKey("CompanyName") Then
            If Not ((cust.Get("CompanyName") = "") Or (cust.Get("CompanyName") = Null)) Then
                lbl.Text = cust.Get("CompanyName")
            End If
        End If

Note that "cust" is a map that may not contain "CompanyName". What's going on?

-- Geoff
 

walterf25

Expert
Licensed User
Longtime User
If I compile the following code in Debug mode, this executes fine. If I compile it as Release, I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference

B4X:
If cust.ContainsKey("CompanyName") Then
            If Not ((cust.Get("CompanyName") = "") Or (cust.Get("CompanyName") = Null)) Then
                lbl.Text = cust.Get("CompanyName")
            End If
        End If

Note that "cust" is a map that may not contain "CompanyName". What's going on?

-- Geoff
How are you saving the Value for Key "CompanyName"?

Try changing Null to "null" as a String
B4X:
If Not ((cust.Get("CompanyName") = "") Or (cust.Get("CompanyName") = "null")) Then
     lbl.Text = cust.Get("CompanyName")
End If

Walter
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you want to test for Null, then you have to do it before the test for an empty string :

B4X:
If cust.ContainsKey("CompanyName") Then
    If cust.Get("CompanyName") <> Null AND cust.Get("CompanyName" <> "" Then
          lbl.Text = cust.Get("CompanyName")
    End If
End If

You should use GetDefault if there is not a valid reason for the string to be empty. That way you don't have to worry about Null.

B4X:
If cust.ContainsKey("CompanyName") AND cust.GetDefault("CompanyName","") <> "" Then
    lbl.Text = cust.Get("CompanyName")
End If

Then you don't really need to check cust.ContainsKey("CompanyName") either unless you are doing other things in the same If block:

B4X:
If cust.GetDefault("CompanyName","") <> "" Then
    lbl.Text = cust.Get("CompanyName")
End If

Will do the same thing.

And, if lbl.Text should be empty if there is no customer it can be simplified further to :
B4X:
lbl.Text = cust.GetDefault("CompanyName","")
 
Last edited:
Upvote 0

geoffschultz

Member
Licensed User
Longtime User
Thanks Stevel05! I knew nothing about the GetDefault method, nor can I find any documentation on it. However, it's relatively obvious and certainly simplified the code.

I still have no understanding of why this code functioned when compiled in Debug mode. That certainly made debugging difficult!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

geoffschultz

Member
Licensed User
Longtime User
Thanks DonManfred! I was searching for "b4a getdefault", which didn't return much. I guess that I should use "b4x" instead of "b4a" in my searches.
 
Upvote 0
Top