Android Question keyvaluestore v2: how to save/retrieve custom type variables values?

Luna Meticcia

New Member
Licensed User
Hi, excuse my poor english.
I've used a List to save and retrieve config params using KVS2: it's all ok: if (on the first App start on a smartphone) there are no config data in "datastore", they are created and saved.
When I have tried to use the same criteria, but with my custom variable type (I find it more... elegant), I get an error about a null reference in the "if m24Config = null" condition:
java.lang.NullPointerException: Attempt to write to field 'int b4a.example.main$_typem24conf.refresh' on a null object reference
Any suggestion? Thanks very much
B4X:
' here we are in the Main module
Sub Process_Globals ' tipi e variabili  dichiarate solo allo start della App e accessibili da TUTTI I moduli
    Type typeNewRecord (ID As Int, titolo As String,intro As String, newUrl As String, imgUrl As String, imgName As String)

    Type typeM24Conf (refresh As Int, totNews As Int, notifiche As Boolean)
    Public m24Conf As typeM24Conf  ' <--- m24Conf is the global custom variable (configuration data)
End Sub

Sub Globals ' tipi e variabili creati ad ogni avvio Activity ed accessibili da questo modulo
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layMain")
    If FirstTime Then
        ' se esiste, recupera configurazione, altrimenti la crea e salva
        m24Conf = Starter.kvs.get("m24Conf")
        If m24Conf = Null Then
            ' m24Conf.Initialize
            m24Conf.refresh = 20 ' <------------ HERE THE EXCEPTION
            m24Conf.totNews = 200
            m24Conf.notifiche = False
            Starter.kvs.Put("m24Conf", m24Conf)
        End If
        StartServiceAt(Serv1, DateTime.Now + DateTime.TicksPerSecond, False) 'chiama webservice
    End If
End Sub
 
Last edited:

Luna Meticcia

New Member
Licensed User
Solved in an inelegant and unelegant way :( (I'm sorry, but I'm working with this useful and beautiful tool -B4A- just from 2 days)
the custom type variable is saved in element 0 of a list:the list works well with keyvaluestore; custom type variable is restored from the element 0 of the list... and can be used anywhere => Main.m24Conf
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layMain")
    If FirstTime Then
        ' se esiste, recupera configurazione, altrimenti la crea e salva
        m24Conf.Initialize
        m24Conf.refresh=20  ' default values (if not in store)
        m24Conf.totNews=200  ' default
        m24Conf.notifiche = False  ' default
        Dim listConf As List ' dummy, local list, just to save/retrieve custom variable
        listConf.Initialize
        Try
            listConf= Starter.kvs.get("m24Conf")
        Catch
            Log("Errore: " & LastException)
        End Try
        If Not(listConf.IsInitialized) Then
            Log( "list no initialized")
            listConf.Initialize
            listConf.Add(m24Conf)
            Starter.kvs.Put("m24Conf", listConf)
        End If
        m24Conf = listConf.Get(0)
        ' --> now m24Conf variable is available with default or saved values <--
        StartServiceAt(Serv1, DateTime.Now + DateTime.TicksPerSecond, False) 'chiama webservice
    End If
End Sub
 
Last edited:
Upvote 0
Top