B4J Question Weird List Add problem

alwaysbusy

Expert
Licensed User
Longtime User
This is probably me, but I just don't see it and it is driving me nuts:

Running this code:
B4X:
    Dim files As List = File.ListFiles(File.Combine(File.DirApp, "VR/moviesR/"))
    For i = 0 To files.Size - 1
        files.Set(i, files.Get(i) & "$R")
    Next
    ' here a load files from another folder
    Dim filesT As List = File.ListFiles(File.Combine(File.DirApp, "VR/moviesT/"))
    For i = 0 To filesT.Size - 1
        ' and want to add them to the first one
        files.Add(filesT.Get(i) & "$T")   '<--- immidiately fails here with the error
    Next

B4X:
Error occurred on line: 64 (Main)
java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.add(AbstractList.java:153)
    at java.base/java.util.AbstractList.add(AbstractList.java:111)
    at anywheresoftware.b4a.objects.collections.List.Add(List.java:71)
    at b4j.example.main._appstart(main.java:166)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    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 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    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 b4j.example.main.start(main.java:38)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

Is the List we get back from File.ListFiles somehow not a normal B4J list?

My workaround currently is using a third list, but I'm just wondering what could be wrong.
B4X:
Dim files As List
    files.Initialize
    
    Dim filesR As List = File.ListFiles(File.Combine(File.DirApp, "VR/moviesR/"))
    For i = 0 To filesR.Size - 1
        files.Add(filesR.Get(i) & "$R")
    Next
    Dim filesT As List = File.ListFiles(File.Combine(File.DirApp, "VR/moviesT/"))
    For i = 0 To filesT.Size - 1
        files.Add(filesT.Get(i) & "$T")
    Next

Alwaysbusy
 

josejad

Expert
Licensed User
Longtime User
Hi:

Without knowing why the error is produced (and that's probably what you want to know, instead another solution), have you tried with:

AddAll (List As List)
Adds all elements in the specified collection to the end of the list.
Note that you can add an array directly.
Example:
List.AddAll(Array As String("value1", "value2"))
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dim files As List = File.ListFiles(File.Combine(File.DirApp, "VR/moviesR/")) For i = 0 To files.Size - 1 files.Set(i, files.Get(i) & "$R") Next
File. ListFiles (Dir As String) As List

Returns a read only list with all the files and directories which are stored in the specified path.
An uninitialized list will be returned if the folder is not accessible.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Yes, I knew that thx, but it needed to add a suffix to each filename. AddAll did not work if I tried to do it to the list returned by listfiles. But now I know it is because it is read only. A .Set did work so it is not fully readonly and what I did not understand.

B4X:
Dim filesR As List = File.ListFiles(File.Combine(File.DirApp, "VR/moviesR/"))
For i = 0 To filesR.Size - 1
    ' does work
    filesR.Set(i, filesR.Get(i) & "$R")
Next

Alwaysbusy
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…