NoSuchElementException encountered when double clicking on right side item

After upgrading twincolselect to 3.3.0 (from 3.2.0), I am getting a NoSuchElementSelection when I double click an entry in the right side…

java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at org.vaadin.tatu.TwinColSelect$TwinColSelectItem.lambda$new$ca65e2c8$3(TwinColSelect.java:260)
at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:244)
at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:501)
. . .

The pick mode is set to DOUBLE. There is no issue double-clicking items on the left side (the items are moved to the right side, as expected).

Thanks, Tom

Version 3.3.0 uses custom drag image, so it requires Vaadin 24.8 or newer, you probably have older version in use.

Apologies for not sharing in my initial note… I ran into the error with each of Vaadin 24.8.2, 24.9.4, and 24.9.5.

That is weird as I have live demo app deployed which is using Vaadin 24.9 at the moment, I did not have this issue with it.

https://v-herd.eu/tatulund-addons/twincolselect

In debugging the TwinColSelect 3.3.0 code, I can see that the click listener is invoked twice, once with a click count of 1 and a second time with a click count of 2.

With the second invocation, I can see that the parent is clearly set (to a VerticalLayout object) until doSwapItems is invoked, at which point the parent is no longer set. Hence, the NoSuchElementException on line 260…

updateDragImage((VerticalLayout) getParent().get());

While debugging with Haijian Wang, we discovered that the comparator is seemingly to blame for the exception (or is, at least, a contributor)…

    TwinColSelectListDataView<Tag> listDataView = twinColSelect.getListDataView();
    listDataView.setSortComparator((a, b) -> a.getName().compareTo(b.getName()));

In other words, the exception doesn’t occur if/when the comparator code is commented out.

Hi @Tatu2 , Here is a series of events on what happens:

  1. User double clicks on an item, and TwinColSelect swaps the items.
  2. The items swapping triggers the filterOrSortingChangedCallback of the TwinColSelectListDataView, which calls the reset method.
  3. In the reset method, the list1 (left vertical layout) removes all the select items, and recreates new ones.
  4. The problematic line updateDragImage((VerticalLayout) getParent().get()); is called in the double click listener. Because it is the old select item that has been removed from list 1, so that the getParent() is empty.

So one possible way of fixing it could be to use getParent().ifPresent(parent -> updateDragImage((VerticalLayout) parent)) instead.

Thanks for debugging and the hint. It is clear by code inspection, but for some strange reason I cannot replicate the issue. I nevertheless released 3.3.1 with the fix. Unfortunately I cannot add automated test as it is not mechanical, probably a timing issue.

1 Like

The fix is in versions 3.3.1 and 4.0.0

1 Like