B4J Question Set values from KeyValueStore when return is null

Acuario

Member
Licensed User
Longtime User
I'm reading values from the KeyValueStore. When the value returns null it seems impossible to test for in a way that seems intuitive.
i.e. the following does not work and the value continues to be null:
B4X:
    Dim deviceID as String
    Dim mykvs As KeyValueStore
    mykvs.Initialize(File.DirApp, "settings")
    deviceID = mykvs.Get("device")
    If deviceID = Null Then deviceID = "test"
    
    If deviceID = "" Then deviceID = "test"
In both cases deviceID remains as null

I know it can be solved in the following way:
B4X:
    Dim o As Object
    mykvs.Initialize(File.DirApp, "settings")
    o = mykvs.Get("device")
    If o = Null Then
        deviceID = "test"
    Else
        deviceID = o
    End If

..but this seems long and just 'feels' wrong..
Is there an alternative way? Maybe a solution would be a method that passes a default value that is returned if the kvs value was null?
 

stevel05

Expert
Licensed User
Longtime User
It should only return Null if you haven't assigned a value to that key (assuming you do not assign Null for some reason), otherwise it will be whatever value you assigned to it.

B4X:
Dim DeviceID As String
If mykvs.ContainsKey("device") Then
       DeviceID = mykvs.Get("device")
    Else
        'Not assigned
       DeviceID = "test"
    End If

Should do it
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
This works because comparisons take the type of the left most value so the right side value is converted to the type of the left, which here is a string.
B4X:
    If deviceID = "null" Then
         deviceID = "test"
    End If
Interestingly the compiler does this at compile time rather than trying to cast Null to a string at runtime, so this must be deliberate behaviour on Erel's part as it needs compile time checks on the comparison type to establish what to do with the Null value

if ((mostCurrent._deviceid).equals("null")) {

You can't use Null as the left value because it causes a null object exception.
 
Upvote 0

Acuario

Member
Licensed User
Longtime User
It should only return Null if you haven't assigned a value to that key (assuming you do not assign Null for some reason), otherwise it will be whatever value you assigned to it.

B4X:
Dim DeviceID As String
If mykvs.ContainsKey("device") Then
       DeviceID = mykvs.Get("device")
    Else
        'Not assigned
       DeviceID = "test"
    End If

Should do it
Ok, thanks to all - handling null values is, in many languages, never what it seems.
I think @stevel05 has a slightly improved way than mine by checking for existence of the key before trying to assign it - obviously the first instance of reading a key in many cases would be a null (usernames/passwords - user data in general comes to mind).

I guess I come from the Windows GetPrivateProfileInt/String etc. where you can specify a default return value in the case of the entry not existing.
Oh well, we live/code with what we have and make the most of it....
 
Upvote 0
Top