Android Question Saving an Array with KVS

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

New question.
I am attempting save an Array using KVS. The KVS is already working with Lists in the App, the Array works OK until I try to save the Array the same way I save the Lists. Is the KVS the correct way to save an Array? Is there something extra needed to save an Array?

Mock-up code below. When the Array "Recording" is associated with KVS, any attempt to store in the Array results in an error.
java.lang.NullPointerException: array == null


Any help greatfully accepted

Regards Roger

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private kvs As KeyValueStore
     
    Dim Recording(16,100,2) As String       
.......
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim  storelist,  As List
.................   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Portrait"
    If FirstTime Then   
        storelist.Initialize
        For i = 0 To 9                               
            storelist.Add(0)
        Next
        Dim DataStore_Flag As Int
        If File.Exists(File.DirDefaultExternal, "datastore") Then DataStore_Flag = 1 Else DataStore_Flag = 0
        kvs.Initialize(File.DirDefaultExternal, "datastore")
        If DataStore_Flag = 0 Then    
            kvs.PutObject("storelist", storelist)
      'kvs.PutObject("Recording", Recording)
...........
        End If
End Sub

Sub Activity_Resume

    storelist = kvs.GetObject("storelist")

    'Recording = kvs.GetObject("Recording")
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    kvs.PutObject("storelist", storelist)
    
    'kvs.PutObject("Recording", Recording)
End Sub
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Erel,

You've done it again. I don't understand why it has to work this way but it does work. :)

Regards Roger

B4X:
Sub Globals
    Dim Recording(16,100,2) As String 
    Dim r2 (,,) As String = kvs.GetObject("recording")
    '... etc
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        kvs.Initialize(File.DirDefaultExternal, "datastore")
        '... etc
    End If
   
    'If VERY FirstTime create file to be read by "Sub Activity_Resume" to stop it spitting the dummy
    Dim DataStore_Flag As Int
    If File.Exists(File.DirDefaultExternal, "datastore") Then DataStore_Flag = 1 Else DataStore_Flag = 0
    kvs.Initialize(File.DirDefaultExternal, "datastore")
    If DataStore_Flag = 0 Then    
        kvs.PutObject("Recording", Recording)
    End If
   
   
    '... etc
End Sub

Sub Activity_Resume
    r2  = kvs.GetObject("Recording")
    'Reload "Recording" array from r2
    For FNM = 0 To 15
        For STEPS = 0 To 99
            For DATA2 = 0 To 1
                Recording(FNM,STEPS,DATA2) = r2(FNM,STEPS,DATA2)
            Next
        Next
    Next
    '... etc   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    kvs.PutObject("recording", Recording)
    '... etc
End Sub
 
Upvote 0
Top