Android Question NewList.get(x) crashing program

rodmcm

Active Member
Licensed User
This code is in a starter module. I connect and see the ESP32 message and the list. On timer tick I extract the variables and use for other activities through refresh subroutines. Every time I use NewList.get() my program crashes. If I just assign strings to the variable then works fine...
Am I doing something wrong with the list call?

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public ESP32Msg As String
    'network stuff   
    Public astream As AsyncStreams
    Public client As Socket
    Public server As ServerSocket
    Public const PORT As Int = 51042
    Public IPAddr As String = "192.168.1.2"
    Public Connected As Boolean
    
    Public Max1 As String
    Public Max2 As Int
    Public Max3 As Int
    Public Max4 As Int
    Public Min1 As Int
    Public Min2 As Int
    Public Min3 As Int
    Public Min4 As Int
    Public Max5 As Int
    
    Dim numbers() As String
    Dim NewList As List
End Sub

Sub Service_Create
Dim RefreshTimer As Timer
    RefreshTimer.Initialize("Timer1",20)                 
    RefreshTimer.Enabled=True
    ConnectToServer(IPAddr)
End Sub

'connects to the server.
Private Sub ConnectToServer(Host As String)
    CloseExistingConnection
    Dim client As Socket
    client.Initialize("client")
    client.Connect(Host, PORT, 10000)
    Wait For Client_Connected (Successful As Boolean)
    If Successful Then
        astream.Initialize(client.InputStream,client.OutputStream, "astream")
        Connected=True
        CallSub2(Main,"StatusUpdate",Connected)
    Else
        Connected=False
        CallSub2(Main,"StatusUpdate",Connected)
    End If
End Sub


' Text Recieved, converted to string and displayed
Sub AStream_NewData (Buffer() As Byte)
         ESP32Msg=BytesToString(Buffer,0,Buffer.Length,"UTF8") 'write buffer as string
        ToastMessageShow(ESP32Msg,False)
        numbers = Regex.Split(",",ESP32Msg)
        NewList.Initialize2(numbers)
        ToastMessageShow(NewList,False)
End Sub


'closes a connection
Sub CloseExistingConnection
  If astream.IsInitialized Then astream.Close
  If client.IsInitialized Then client.Close
End Sub


Sub AStream_Error

End Sub

Sub AStream_Terminated

End Sub

Sub Timer1_Tick
    ExtractVariables
    CallSub(PlayText,"PlayText_Refresh")
End Sub
'---------

Sub ExtractVariables
    Max1="54"
    Max1=NewList.get(1)          ''
    'Min1=NewList.get(2)                     ''
    'Max2=NewList.get(3)                      ''
    'Min2=NewList.get(4)
    'Max3=NewList.get(5)
    'Min3=NewList.get(6)
    'Max4=NewList.get(7)
    'Min4=NewList.get(8)
End Sub
 

rodmcm

Active Member
Licensed User
Process: b4a.example, PID: 21861

java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (List).

at anywheresoftware.b4a.BA.raiseEvent2(BA.java:223)

at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)

at android.os.Handler.handleCallback(Handler.java:815)

at android.os.Handler.dispatchMessage(Handler.java:104)

at android.os.Looper.loop(Looper.java:207)

at android.app.ActivityThread.main(ActivityThread.java:5765)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Caused by: java.lang.RuntimeException: Object should first be initialized (List).

at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)

at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)

at b4a.example.starter._extractvariables(starter.java:270)

at b4a.example.starter._timer1_tick(starter.java:333)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
 
Upvote 0

rodmcm

Active Member
Licensed User
Took me a while to realise that I had to untick filter to get error messages !

Seems as if the initalisation of the list is in the wrong place. Where should it go if it is initialised by 'numbers' the size of which is not know until the message is received?
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
start the timer after you receive message or initialize an empty list at service create

also timer must be declared at process_globals

B4X:
Sub Process_Globals
Dim RefreshTimer As Timer
End sub

B4X:
Sub Service_Create
    RefreshTimer.Initialize("Timer1",20)                
    RefreshTimer.Enabled=True
    ConnectToServer(IPAddr)
Newlist.Initialize  ' <-----
End Sub

or


B4X:
Sub AStream_NewData (Buffer() As Byte)
         ESP32Msg=BytesToString(Buffer,0,Buffer.Length,"UTF8") 'write buffer as string
        ToastMessageShow(ESP32Msg,False)
        numbers = Regex.Split(",",ESP32Msg)
        NewList.Initialize2(numbers)
        ToastMessageShow(NewList,False)

       
 RefreshTimer.Initialize("Timer1",20)     '<------           
    RefreshTimer.Enabled=True
End Sub
 
Upvote 0
Top