B4J Question List from Array and then Add = Exception?

tchart

Well-Known Member
Licensed User
Longtime User
I have a list that is created like this;

Dim Header As List = Array("Name", "Status", "Public IP", "Control")

I've found that if I then do this;

Header.Add("New")

B4J 5.5 is throwing this exception;

Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at anywheresoftware.b4a.objects.collections.List.Add(List.java:71)

Are lists created like this read only?
 

Daestrum

Expert
Licensed User
Longtime User
It looks like a list created this way has a fixed length and other limits, ie you can't remove an item from it.
Probably safer to do
B4X:
Dim Header as List
...
Header.Initialize
Header.AddAll(Array("Name", "Status", "Public IP", "Control"))
...
Header.Add("New")
...
Then it works as expected.

Also you can force it to become a normal list with 4 lines of code
B4X:
Dim tempList As List
tempList.Initialize
tempList.AddAll(originalList)
originalList = tempList ' now it will work as normal list
...
But this will create a second list mirroring the first one, so first solution is better.
 
Last edited:
Upvote 0
Top