Calling clear () on ObservableList throws IndexOutOfBoundsException

I have a ComboBox with an observable list box that updates as the user types in characters or makes a selection. The problem I am facing is when I select an item from the ComboBox and my listener event is fired which then calls the clear () method from the ComboBox ObservableList.

FULL CODE

    public void suggestItem(ActionEvent ev){
    String currentInput = foodSearch.getEditor().getText();
    if(currentInput.length() > 4){
        DatabaseCommunicator.openConnection();

        // Returns a list no greater than size 5 of possible food items
        ArrayList<String> foodList = DatabaseCommunicator.findSimilarFoods(currentInput);

        ObservableList<String> comboList = foodSearch.getItems();
        comboList.setAll(foodList);

        DatabaseCommunicator.closeConnection();
    }

}

      

Now when I get the error, the ObservableList appears as it should, but I still get this exception. Trying to debug this caused my IDE to freeze after calling setAll which triggers clear () and I had to kill the IDE via terminal.

If I replace setAll with addAll that hasn't cleared (), I don't get any exceptions and my list is updated with the added element added.

I was unable to catch the exception in the Listener, which I was expecting from looking at the stack trace, but wanted to try it anyway.

Here is the stop track.

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
    at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)
    at javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:242)
    at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new$178(ListViewBehavior.java:264)
    at com.sun.javafx.scene.control.behavior.ListViewBehavior$$Lambda$321/1588822558.onChanged(Unknown Source)
    at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
    at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:75)
    at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:331)
    at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1385)
    at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:260)
    at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:224)
    at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3719)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3447)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1723)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2456)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$347(GlassViewEventHandler.java:385)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$169/414422402.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:387)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:549)
    at com.sun.glass.ui.View.notifyMouse(View.java:921)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1091223379.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

      

+3


source to share


1 answer


This is a bit of a hack, but

Platform.runLater(() -> {
        ObservableList<String> comboList = foodSearch.getItems();
        comboList.setAll(foodList);
}

      



seems to fix this. I think the problem is that you are changing the list and the selection model is changing the list selectedItems

, which violates the rule that you don't change the observable list during notifications. Usage Platform.runLater(...)

delays your modification until the current event has been fully processed.

+9


source







All Articles