0

I have a TableView courseTableView which stores several Course objects

enter image description here

I also have a method selectCourse(), which allows one to click on a row and get the information about the respective course to a TextField courseName, allowing it to be modified.

However, when I try to deselect the selected row using courseTableView.getSelectionModel().clearSelection();, I get an IndexOutOfBoundsException

Here is the entire error message:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
    at javafx.base/javafx.collections.transformation.SortedList.get(SortedList.java:168)
    at javafx.controls/com.sun.javafx.scene.control.SelectedCellsMap.get(SelectedCellsMap.java:77)
    at javafx.controls/javafx.scene.control.TableView$TableViewArrayListSelectionModel$3.get(TableView.java:2121)
    at javafx.controls/javafx.scene.control.TableView$TableViewArrayListSelectionModel$3.get(TableView.java:2119)
    at Interface.ManageData$1.onChanged(ManageData.java:202)
    at javafx.base/com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
    at javafx.base/com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at javafx.base/javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
    at javafx.controls/com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:110)
    at javafx.controls/javafx.scene.control.TableView$TableViewArrayListSelectionModel.fireCustomSelectedCellsListChangeEvent(TableView.java:3033)
    at javafx.controls/javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearSelection(TableView.java:2803)
    at Interface.ManageData.unselectCourse(ManageData.java:210)
    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:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    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.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    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.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$MouseHandler.process(Scene.java:3862)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.ja**va:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    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:830)

But even thought I get this error, the code still executes and the row is deselected. Here is the selectCourse()

public void selectCourse(){                     
    courseTableView.getSelectionModel().setCellSelectionEnabled(true);
    ObservableList selectedCells = courseTableView.getSelectionModel().getSelectedCells();
      selectedCells.addListener(new ListChangeListener() {
      @Override
      public void onChanged(Change c) {
        TablePosition tablePosition = (TablePosition) selectedCells.get(0);
        Object val = tablePosition.getTableColumn().getCellData(tablePosition.getRow());
        courseName.setText(val.toString());
      }
    });
  }**

And the unselectCourse() (which is temporarily bound to the 'Remove' Button)

 public void unselectCourse(){
    courseTableView.getSelectionModel().clearSelection();
  }

I am confident the issue lies in the selectCourse() method but I am unable to find a solution to fix it.

Thank you!

Silent_Jager
  • 111
  • 1
  • 1
  • 7
  • 1
    "I am confident the issue lies in the `selectCourse()`". Your stack trace says otherwise: `at Interface.ManageData.unselectCourse(ManageData.java:210)`. The error seems to be in `onChanged`, most likely here: `selectedCells.get(0);` – Costi Ciudatu Dec 08 '19 at 12:17
  • 4
    Please [edit] your question to add a [mre] demonstrating the problem. Also see [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/). Though that being said, Costi is probably right and the call to `get(0)` is the problem. You've registered a list-change listener to the selection model, clear the list (which makes the list empty), and then fail to check if there's an element at index `0` before attempting to retrieve it. – Slaw Dec 08 '19 at 12:21
  • You were correct, thank you. I fixed it by wrapping it inside `if(selectedCells.size()>0)` – Silent_Jager Dec 08 '19 at 12:33
  • Another thing to note: You're adding a new `ListChangeListener` every time `#selectCourse()` is invoked; this will result in more and more listeners, all doing the same thing, being executed when the list is modified. – Slaw Dec 09 '19 at 22:37

1 Answers1

1

You may want to change your ListChangeListener so it checks if the selection is empty, before selecting the first item

selectedCells.addListener(new ListChangeListener() {
  @Override
  public void onChanged(Change c) {
    if(!selectedCells.isEmpty()) {
    TablePosition tablePosition = (TablePosition) selectedCells.get(0);
    Object val = tablePosition.getTableColumn().getCellData(tablePosition.getRow());
    courseName.setText(val.toString());
  }
}
});
AdminOfThis
  • 191
  • 3