Bug? Testing string value to Null

b4auser1

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim l_sSimpleValue As String = Null
    Log($"l_sSimpleValue = ${l_sSimpleValue}"$)

    If l_sSimpleValue <> Null Then
        Log($"l_sSimpleValue <> Null"$)
    Else
        Log($"l_sSimpleValue = Null"$)
    End If

    If l_sSimpleValue.CompareTo(Null) = 0 Then
        Log($"l_sSimpleValue.CompareTo(Null) = 0"$)
    Else
        Log($"l_sSimpleValue.CompareTo(Null) <> 0"$)
    End If

Log output:
l_sSimpleValue = null
l_sSimpleValue <> Null
l_sSimpleValue.CompareTo(Null) = 0

P.S. The same in B4A and B4J.

Note 1: Really I discovered the problem when ported code from B4A to B4i.
l_sSimpleValue = ExecQuerySingleResult2, ExecQuerySingleResult ' return null
In B4A, B4J
If l_sSimpleValue <> Null Then ...
with null returned from ExecQuerySingleResult2, ExecQuerySingleResult to l_sSimpleValue gives true.

In B4i
If l_sSimpleValue <> Null Then ...
with null returned from ExecQuerySingleResult2, ExecQuerySingleResultto l_sSimpleValue gives false.

but if we assign Null to string value directly, the behavior in B4A, B4J, B4i the same (see. code and results above).

Note 2: According the documentation:
B4A, B4J: ExecQuerySingleResult2, ExecQuerySingleResult Returns Null if no results were found.
B4i: Returns an empty string if no value was found.
But really in B4i ExecQuerySingleResult2, ExecQuerySingleResult return null instead of empty string.

l_sSimpleValue = ExecQuerySingleResult2, ExecQuerySingleResult
Log($"l_sSimpleValue = ${l_sSimpleValue}"$)
Log output:
l_sSimpleValue = null

As a workaround I will try to use l_sSimpleValue.CompareTo(Null) = 0 instead of If l_sSimpleValue <> Null Then, but it will force me do a lot of changes to the code :(.

It would great to make behaviour of l_sSimpleValue <> Null and l_sSimpleValue.CompareTo(Null) = 0 equal, fix documentation for iSQL "Returns an empty string if no value was found." => "Returns Null if no results were found." or understand why ExecQuerySingleResult2, ExecQuerySingleResult return null (instead of empty string) and why this "null" is compared to Null in different way in B4A, B4J and B4i.
 

b4auser1

Well-Known Member
Licensed User
Longtime User
Workaround to test if String = Null
B4X:
Public Sub StringIsNull(a_sValue As String) As Boolean
    Return a_sValue = Null Or a_sValue.CompareTo(Null) = 0
End Sub
 

b4auser1

Well-Known Member
Licensed User
Longtime User
It does return an empty string in B4i:
B4X:
Dim s As SQL
s.Initialize(File.DirLibrary, "1.db", True)
s.ExecNonQuery("CREATE TABLE table1 (col1 INT)")
Log("test: " & s.ExecQuerySingleResult("SELECT col1 FROM table1"))
Excuse me. I found error in my DB related code.

How do you think to do behavior of
l_sSimpleValue <> Null
and
l_sSimpleValue.CompareTo(Null) = 0
the same ?
 

Phil Griffiths

New Member
Licensed User
Longtime User
I experienced the same issue, however I do not consider myself qualified to say whether this is a bug or not...although the behaviour is certainly confusing...

Example:

My database table contains an integer field which has a value of null.

I use a call to DBUtils.ExecuteMap to populate a Map 'Datamap'

then in my code

Dim VisitCount As Int
VisitCount = Datamap.Get("visitcount")

returned an error - invalid double = null

So I reasoned that if I read in a string rather than an integer then I could the string test for a null value

Dim VisitCountStr As String
Dim VisitCount As Int
VisitCountStr = Datamap.Get("visitcount")
If VisitCountStr <> Null Then
VisitCount = VisitCountStr 'let b4a take care of conversion between string and integer
Else
VisitCount = 0
End If

I debugged the code checking the value of variables:

VisitCountStr was equal to 'null' (note the lower case value)
The program interpreted VisitCountStr as not being equal to 'Null' so it executed the next line and the program stopped with an invalid double = null error.

I then used the workaround provided by b4auser1 - and this worked fine. (thank you b4auser1 ;-) )

Why does 'null' retrieved from database not equate to the Null keyword in b4a?
 
Top