B4J Question xcustomlistview 1.7, error deleting last item

tohtics

Member
Licensed User
i got the following error when i delete the last item in a xcustolistview
customlistview._getrawlistitem (java line: 445)
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:105)
at b4j.example.customlistview._getrawlistitem(customlistview.java:445)
at b4j.example.customlistview$ResumableSub_PanelClickHandler.resume(customlistview.java:771)
at b4j.example.customlistview._panelclickhandler(customlistview.java:747)
at b4j.example.customlistview._panel_mouseclicked(customlistview.java:738)
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.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 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
My code is
B4X:
Sub imvCerrar_MouseClicked (EventData As MouseEvent)
    'Dim xyz As B4XView = Sender   
    Dim Index As Int = clv1.GetItemFromView(Sender)
    Log("index: " & Index)
    clv1.RemoveAt(Index)

    If clv1.Size > 0 Then
        Sleep(0)
        recontar(0)
    End If
End Sub
"ava.lang.IndexOutOfBoundsException: Index x out of bounds for length x", x change acording to the number of elements in the xcustomlistview
 

tohtics

Member
Licensed User
Well, i can move or eliminate that sentence, sleep(0), but the error persists, no matter how many items exist, as soon its deleted the last one, the error ocurrs. In debug mode the program does not break, but its detected by the compiler, just in release mode the program breaks.
 

OliverA

Expert
Licensed User
I'm guessing here: recontar sub redisplays the custom list view at position 0 (recontar(0)). The issue is, if you deleted the last item, position 0 may not exist. You may want to add bounds checking in recontar and do nothing if clv1.size is 0.

B4X:
Sub recontar(index As Int)
   If clv1.size = 0 Then Return
   'Original code follows here
   '...
End Sub
 

Erel

Administrator
Staff member
Licensed User
The error happens because of the ItemClick event that is also fired.
I assume that you are handling it as well.

You can do something like this to remove the item after the selection was completed:
B4X:
Sub imvCerrar_MouseClicked (EventData As MouseEvent)
    'Dim xyz As B4XView = Sender   
    Dim Index As Int = clv1.GetItemFromView(Sender)
    Sleep(250)
    Log("index: " & Index)
    clv1.RemoveAt(Index)

    If clv1.Size > 0 Then
        Sleep(0)
        recontar(0)
    End If
End Sub
 
Top