Bug? B4XOrderedMap bug ?

rdkartono

Member
Licensed User
Longtime User
Given this code :

B4X:
Dim devicemap As B4XOrderedMap
..........

Public Sub clearing_all_audioinputs_from_devicemap
    If devicemap.IsInitialized=False Then devicemap.Initialize
    If devicemap.Size = 0 Then Return
    
    
    For Each desc As String In devicemap.Keys
        Dim devstat As devicestatus = devicemap.Get(desc)
        If devstat<>Null Then
            If devstat.IsInitialized Then
                If devstat.IsInputDevice Then
                    devstat.CloseCommunication
                    devicemap.Remove(desc)
                End If
            End If
        End If
    Next
End Sub

B4XOrderedMap will generate error with following message :
B4X:
Error occurred on line: 24 (B4XOrderedMap)
java.lang.IndexOutOfBoundsException: Index: 110, Size: 110
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:105)
    at gtc.qzaclient.main._clearing_all_audiooutputs_from_devicemap(main.java:648)
    at gtc.qzaclient.audiooutputformclass$ResumableSub_inittable_Click.resume(audiooutputformclass.java:1075)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    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.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    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.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:136)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:85)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:216)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native Method)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplication.java:204)
    at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:511)
    at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:590)
    at javafx.stage.Stage.showAndWait(Stage.java:474)
    at anywheresoftware.b4j.objects.Form.ShowAndWait(Form.java:215)
    at gtc.qzaclient.audiooutputformclass._initialize(audiooutputformclass.java:77)
    at gtc.qzaclient.main$ResumableSub_MenuBar1_Action.resume(main.java:1285)
    at gtc.qzaclient.main._menubar1_action(main.java:1057)
    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.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    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.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:216)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

I think "For Each" looping and "removing" need to be checked again.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not a bug. You cannot remove items from a collection inside a For Each loop that iterates over its items. A similar error will happen with all collections.
B4XOrderedMap actually makes this task possible without relying on another collection:
B4X:
Dim i As Int
Do While i < devicemap.Size
   Dim desc As String = devicemap.Keys.Get(i)
   Dim devstat As Object = devicemap.Get(desc)
   If ... Then
       devicemap.Remove(desc)
       i = i - 1
   End If
   i = i + 1
Loop
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…