Android Question checking null condition

leongcc

Member
Licensed User
Longtime User
I am beginning to have problem checking null condition.
Please consider the sets of codes below.
The log shows the following for both:
something1
nothing2

I appreciate if someone can explain why "(s=Null)" is False.

B4X:
Sub Activity_Resume
    Dim s As String
    s = test
   
    If (s=Null) Then
        Log("nothing1")
    Else
        Log("something1")
    End If
   
    If (s="null") Then
        Log("nothing2")
    Else
        Log("something2")
    End If
End Sub

Sub test As String
    Return  Null
End Sub

B4X:
Sub Activity_Resume
    Dim s As String = Null
   
    If (s=Null) Then
        Log("nothing1")
    Else
        Log("something1")
    End If
   
    If (s="null") Then
        Log("nothing2")
    Else
        Log("something2")
    End If
End Sub
 

JTmartins

Active Member
Licensed User
Longtime User
If you do

Dim S as object

Sub test as Object

I think you get the result you want, because NULL is not a string.
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Resume
    private s = "" As String
 
    If s.Length < 1 Then
        Log("nothing1")
    Else
        Log("something1")
    End If
 
    If s.Length < 1 Then
        Log("nothing2")
    Else
        Log("something2")
    End If
End Sub


or

B4X:
Sub Activity_Resume
    private s = "" As String
 
    If s = "" Then
        Log("nothing1")
    Else
        Log("something1")
    End If
 
    If s = "" Then
        Log("nothing2")
    Else
        Log("something2")
    End If
End Sub


or

B4X:
Sub Activity_Resume
    private s = "" As String 

    If s.Length = 0 Then
        Log("nothing1")
    Else
        Log("something1")
    End If

    If s.Length = 0 Then
        Log("nothing2")
    Else
        Log("something2")
    End If
End Sub
 
Upvote 0

leongcc

Member
Licensed User
Longtime User
Thanks. All the solutions are good but why won't "(s=Null)" work ?

There are situations when it works, for example the following generates "nothing" when ReadLine returns a null string.

B4X:
Dim Reader As TextReader
Dim s as String
s = Reader.ReadLine
If (s=Null) Then
   Log("nothing")
Else
    Log("something")
End If
 
Last edited:
Upvote 0

leongcc

Member
Licensed User
Longtime User
The reason why I am stuck is I have just built a subprogram that might returns a Null.
The caller has to check if the Sub returns a Null.

If we run the following codes, we can see that:
1) s="null" is true, but "null" length is 4 bytes, strange!
2) s="Null" is false
3) s = Null is false

I have used libraries functions that returns Null and I have no problem checking Null by just:
if (s=Null) then
How is it done ?


B4X:
Sub Activity_Resume
    Dim s As String
    s = test
   
    If (s=Null) Then
        Log("nothing1")
    Else
        Log("something1")
    End If
   
    If (s="null") Then   
        Log("nothing2")
    Else
        Log("something2")
    End If
   
    If (s="Null") Then 
        Log("nothing3")
    Else
        Log("something3")
    End If
End Sub

Sub test As String
    Return  Null
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Resume
    Dim s As String
    s = test

    If s=Null Then ' this should work
        Log("nothing1")
    Else
        Log("something1")
    End If

    If s="null" Then  ' you are comparing s with the string "null"... s will be converted into a string. null will be "Null" then; a 4 byte string... comparing = false
        Log("nothing2")
    Else
        Log("something2")
    End If

    If s="Null" then  ' you are comparing s with the string "Null"... s will be converted into a string. null will be "Null" then; a 4 byte string... comparing = true
        Log("nothing3")
    Else
        Log("something3")
    End If
End Sub

Sub test As String
    Return  Null
End Sub

with your code If (s="Null") then
you used ( and ) so android will check the content of () first to return true or false. to compare things they must have the same type. Android will convert both values inside () to a string to compare them...

just my 2 cent (my understanding on the behavior)
 
Last edited:
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
It appears that strings cannot be set to Null. When you return Null from a function, it seems it is being cast to a string by setting it to the literal characters 'n-u-l-l'. When comparing with If s=Null, it will fail since s is never Null, either it is an empty string or contains the "null" characters. When comparing with If s="Null", it will fail because "Null" does not equal "null". So instead of returning Null from the function, you probably need to return an empty string instead, then compare using one of the methods Douglas suggests.

B4X:
Sub test As String
    Return ""
End Sub
 
Upvote 0
Top