B4J Question List.RemoveAt(i) generates out of bounds exception

Nokia

Active Member
Licensed User
Longtime User
when trying to remove list item, getting out of bounds exception.
any ideas on how to get index?

loading list:
B4X:
    Dim su As StringUtils
    EmailList = su.LoadCSV(Main.WrkFld, csvName, csvChar)

trying to remove item from list:
B4X:
    Dim I As Int = 0
    
    For Each Row() As String In EmailList
        For Each Col As String In Row
            If Col = EmailToRemove Then
                EmailList.RemoveAt(I)
                Exit
            End If
        Next
        I = I + 1
    Next

this is the error that generates:

Error occurred on line: 328 (clsEmail)
java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:105)
at b4j.example.clsemail._removefromemaillist(clsemail.java:847)
at b4j.example.clsemail._ivnode_mouseclicked(clsemail.java:809)
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:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.GeneratedMethodAccessor9.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.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:93)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot remove items from the list inside a For Each loop that iterates over the same list.
B4X:
Dim index As Int
Do While index < EmailList.Size
 Dim row() As String = EmailList.Get(index)
 
  For Each Col As String In Row
            If Col = EmailToRemove Then
                EmailList.RemoveAt(index)
                index = index - 1
                Exit
            End If
      Next

 index = index + 1
Loop
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You cannot remove items from the list inside a For Each loop that iterates over the same list.
B4X:
Dim index As Int
Do While index < EmailList.Size
Dim row() As String = EmailList.Get(index)

  For Each Col As String In Row
            If Col = EmailToRemove Then
                EmailList.RemoveAt(index)
                index = index - 1
                Exit
            End If
      Next

index = index + 1
Loop


Thanks Erel

was not sure how I was going to get the index out of a list setup with columns.. I see now.. instead of looping through all rows with columns,, you are getting each row separately called by the get(index)..
 
Upvote 0
Top