yaniv hanya Active Member Licensed User Aug 13, 2019 #1 I have this code Code: Copy If currentFilesLst.Size>0 Then currentFilesLst.Clear End If currentFilesLst is alist of files names and has data. when it gets to the currentFilesLst.Clear it crush and i get this log Error occurred on line: 112 (Main) java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:161) at java.util.AbstractList$Itr.remove(AbstractList.java:374) at java.util.AbstractList.removeRange(AbstractList.java:571) at java.util.AbstractList.clear(AbstractList.java:234) at anywheresoftware.b4a.objects.collections.List.Clear(List.java:69) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777) at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354) at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144) at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180) at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262) at b4a.example.main._fillmainlist(main.java:566) at b4a.example.main._finedview(main.java:1199) at b4a.example.main._img3_click(main.java:1467) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732) at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351) at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144) at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180) at anywheresoftware.b4a.BA.raiseEvent(BA.java:176) at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80) at android.view.View.performClick(View.java:5675) at android.view.View$PerformClick.run(View.java:22641) at android.os.Handler.handleCallback(Handler.java:836) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) Click to expand... it worked before and i cann't understend what hapend what should i chck?
I have this code Code: Copy If currentFilesLst.Size>0 Then currentFilesLst.Clear End If currentFilesLst is alist of files names and has data. when it gets to the currentFilesLst.Clear it crush and i get this log Error occurred on line: 112 (Main) java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:161) at java.util.AbstractList$Itr.remove(AbstractList.java:374) at java.util.AbstractList.removeRange(AbstractList.java:571) at java.util.AbstractList.clear(AbstractList.java:234) at anywheresoftware.b4a.objects.collections.List.Clear(List.java:69) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777) at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354) at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144) at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180) at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262) at b4a.example.main._fillmainlist(main.java:566) at b4a.example.main._finedview(main.java:1199) at b4a.example.main._img3_click(main.java:1467) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732) at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351) at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255) at java.lang.reflect.Method.invoke(Native Method) at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144) at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180) at anywheresoftware.b4a.BA.raiseEvent(BA.java:176) at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80) at android.view.View.performClick(View.java:5675) at android.view.View$PerformClick.run(View.java:22641) at android.os.Handler.handleCallback(Handler.java:836) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) Click to expand... it worked before and i cann't understend what hapend what should i chck?
E emexes Expert Licensed User Longtime User Aug 13, 2019 #2 To diagnose the problem, try: Code: Copy Do While CurrentFilesLst.Size > 0 Log( CurrentFilesList.Get(0) ) CurrentFilesList.RemoveAt(0) Loop ... and if that works (wtf?) then perhaps just delete the Log line and it's: job done! One possibility is that you've added the list to itself (I'm pretty sure recursion is allowed) and when that particular item is deleted, it... well, I don't know what would happen, but I'm sure there would be some complaining about it aka error message!!! Last edited: Aug 13, 2019 Upvote 0
To diagnose the problem, try: Code: Copy Do While CurrentFilesLst.Size > 0 Log( CurrentFilesList.Get(0) ) CurrentFilesList.RemoveAt(0) Loop ... and if that works (wtf?) then perhaps just delete the Log line and it's: job done! One possibility is that you've added the list to itself (I'm pretty sure recursion is allowed) and when that particular item is deleted, it... well, I don't know what would happen, but I'm sure there would be some complaining about it aka error message!!!
Erel B4X founder Staff member Licensed User Longtime User Aug 13, 2019 #4 It will not work. See File.ListFiles documentation: https://www.b4x.com/android/forum/pages/results/?query=file.listfiles It returns a read-only list. If you need to modify it: Code: Copy Dim list1 As list1 list1.Initialize list1.AddAll(File.ListFiles(...)) Upvote 0
It will not work. See File.ListFiles documentation: https://www.b4x.com/android/forum/pages/results/?query=file.listfiles It returns a read-only list. If you need to modify it: Code: Copy Dim list1 As list1 list1.Initialize list1.AddAll(File.ListFiles(...))
yaniv hanya Active Member Licensed User Aug 13, 2019 #5 Thanks That explains everything. Everything worked fine until I decided to clear it Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Aug 13, 2019 #6 In the sake of completeness, lists that are based on arrays are also read-only: Code: Copy Dim MyList As List = Array(1, 2, 3, 4) MyList.Add(5) 'crash! Dim MyList2 As List MyList2.Initialize MyList2.AddAll(Array(1, 2, 3, 4)) MyList2.Add(5) 'does not crash Upvote 0
In the sake of completeness, lists that are based on arrays are also read-only: Code: Copy Dim MyList As List = Array(1, 2, 3, 4) MyList.Add(5) 'crash! Dim MyList2 As List MyList2.Initialize MyList2.AddAll(Array(1, 2, 3, 4)) MyList2.Add(5) 'does not crash