Android Question KeyValueStore2 error, null pointer exception

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I tried to use KeyValueStore2 in B4A 9.5

On my service start, this is the codes
B4X:
Sub Service_Create
    Private rp As RuntimePermissions
    Main.PUB_InstDir= rp.GetSafeDirDefaultExternal("")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    Try
        If File.Exists(Main.PUB_InstDir,"kvs.dat") =  False Then
            Main.kvs.Initialize(Main.PUB_InstDir,"kvs.dat")
            Main.kvs.PutEncrypted("RecNo",0,Main.PASSWORD)
        End If
    Catch
        ExitApplication
    End Try
End Sub

On main
B4X:
Sub Process_Globals
    Public Const PASSWORD As String = "1234567"
    Public PUB_InstDir As String
    Public kvs As KeyValueStore
End Sub

Sub btn_Clicked
    kvs.PutEncrypted("RecNo",1,PASSWORD)
    Log(1)
End Sub

On btn_clicked, raised an error
java.lang.NullPointerException
at b4a.example3.keyvaluestore._putencrypted(keyvaluestore.java:421)


Am I missing something here?
 

incendio

Well-Known Member
Licensed User
Longtime User
Why aren't you initializing KVS if the file exists? This is probably the cause of this error.

You should initialize it and then check if the key exists with KVS.ContainsKey.
You were right. Changed the codes in service start, now running OK, thanks.
 
Upvote 0
Top