B4J Question loop in map raise an error

Addo

Well-Known Member
Licensed User
Longtime User
i am using a Timer to loop Through map periodically as Following

B4X:
Private Sub AudioClientsTmr_tick

If AudioClients.Size > 0 Then
    
For i =  AudioClients.Size -1 To 0 Step -1

Dim Audioc As AudioClient

Audioc = AudioClients.Get(i)

Log(Audioc.Active)

    
Next
    
    
    
End If
    
    
End Sub


i got the following exception

main._appstart (java line: 60)
java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:120)
at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:153)
at Tcp.Server.main._appstart(main.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at Tcp.Server.main.main(main.java:28)
Caused by: java.lang.NullPointerException
at Tcp.Server.audioserver._audioclientstmr_tick(audioserver.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
... 12 more

what i am doing wrong ?
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Get uses the key to the map and it not an index. GetValueAt is available on some platforms.
However, the preferred way to do this is:

B4X:
Private Sub AudioClientsTmr_tick
If AudioClients.Size > 0 Then
 ' Get all map values'
for each Audioc As AudioClient in audioclients.values
    
Log(Audioc.Active)
   
Next
    
End If

also if you use this method there is no need for the size check as if there are no values the For each will just complete.
 
Upvote 0
Top