What's wrong with this code: java.lang.UnsupportedOperationException

junglejet

Active Member
Licensed User
Longtime User
I am storing entries in a string with : as a separator. The string is split into a list and can be viewed and individual items slected from an inputlist.

To add items to the list I use the code below. On its first execution an exception appears:
main_bu_fav_longclick (B4A line: 1960)
slist.Clear
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:645)
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:77)
at java.util.AbstractList.removeRange(AbstractList.java:665)
at java.util.AbstractList.clear(AbstractList.java:473)
at anywheresoftware.b4a.objects.collections.List.Clear(List.java:66)
at xxx.xxx.main._bu_fav_longclick(main.java:1294)

Also an "slist.add("CCCC") at the same place as "slist.clear" throws the same exception.

When I call the code a second time and for all subsequent times the code works ok.

What's wrong here?

B4X:
'edwx defined as edittext above
Dim wxlist as string : wxlist="AAAA:BBBB"

Sub bu_fav_longclick 'Add a favorite
   Dim i,j As Int
   Dim slist As List
   slist.Initialize
   If wxlist.Length=0 Then
      wxlist=edwx.Text
      Return
   End If
   slist=Regex.Split(":",wxlist)
   i=slist.IndexOf(edwx.Text)
   If i>-1 Then
      Return
   End If
   wxlist=wxlist&":"&edwx.Text
   slist.Clear
   slist=Regex.Split(":",wxlist)
   slist.Sort(True)
   wxlist=""
   j=slist.Size-2
   For i=0 To j
      wxlist=wxlist&slist.Get(i)&":"
   Next
   wxlist=wxlist&slist.Get(slist.size-1)   
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. You do not need to clear the list as you are assigning a new list to this variable in the next line.
2. Regex.Split returns an array. When you assign an array into a list variable it gets wrapped into a fixed size list (based on the same array). So you cannot call Clear on this list.

If you want to convert the array into a dynamic list then you should call List.AddAll(Regex.Split(...))

However in this case you should just remove List.Clear.
 
Upvote 0

junglejet

Active Member
Licensed User
Longtime User
:sign0148:

Thanks, Erel, great support as usual.

I will try to remember in future that (and document here for my aging brain)
- Regex.split returns an array, not a list
- an array is converted to a FIXED size list by implicit conversion to a list
- such lists cannot be altered (as the name says)

Best
Andy
 
Upvote 0
Top