B4J Question [solved] Test Null?

MarkusR

Well-Known Member
Licensed User
its a bug?
output is
null
null
B4X:
'B4J 7.51
Sub Test2
    
    Dim Params As Map
    Params.Initialize
    'Params.Put("Key","ABC") 'without this row .Get Return something that can Not be Test As null
    
    Dim MyString As String
    MyString = Params.Get("Key")
    Log(MyString)
    'If IsNull(MyString) Then MyString = "123"
    If MyString = Null Or MyString = "" Then MyString = "123"
    Log(MyString)
    
End Sub

Sub IsNull(O As Object) As Boolean
    Return (O=Null)
End Sub
 

amykonio

Member
Licensed User
its a bug?
output is
null
null
B4X:
'B4J 7.51
Sub Test2
 
    Dim Params As Map
    Params.Initialize
    'Params.Put("Key","ABC") 'without this row .Get Return something that can Not be Test As null
 
    Dim MyString As String
    MyString = Params.Get("Key")
    Log(MyString)
    'If IsNull(MyString) Then MyString = "123"
    If MyString = Null Or MyString = "" Then MyString = "123"
    Log(MyString)
 
End Sub

Sub IsNull(O As Object) As Boolean
    Return (O=Null)
End Sub
Hi.
What happens if you change:
B4X:
MyString = Null
to
B4X:
MyString = "null"

Does it work?

Anyway. The way I would write it is
B4X:
Sub Test2
 Dim Params As Map
 Dim o As Object
 Params.Initialize
' Params.Put("Key","ABC") 'without this row .Get Return something that can Not be Test As null
 
 Dim MyString As String
 o = Params.Get("Key")
 If o Is Object Then
  MyString = o
 Else
  MyString = "123"
 End If
 Log(MyString)
End Sub
 

MarkusR

Well-Known Member
Licensed User
The Null value is converted to "null" string.
The less you work with nulls the better, especially with strings. It is better to write it like this:
B4X:
Dim MyString As String = Params.GetDefault("Key", "")
If MyString = "" Then ...
hi erel, ahhh :)
that Null is converted to "null" string is special.
because missing quotes maybe you can modify this window that is it more clear what kind of value is displayed.
MyString null String
MyString null Object
or
MyString = "null" to be equal to source code


Snap_2019.07.18_09h20m10s_002_.png
 

amykonio

Member
Licensed User
I'm not sure this would be helpful...
All values for strings in this window are displayed without quotes. Also, how can we know if the value "null" wasn't set as a string?
This is the main reason I believe Erel suggests to avoid working with nulls - strings...
 

MarkusR

Well-Known Member
Licensed User
intuitiv i would expect empty string "" if there is no key found. and a method Params.KeyExists("Key")
other languages would raise an error but i not like error handling for every method.
B4X:
Dim MyString As String
MyString = Params.Get("Key")
 
Top