Error after deleting Grid row(s)

Using Grid with an SQLContainer data source, in multi-select mode.

Delete one or more items from the container, the corresponding Grid’s rows are removed as expected. But all subsequent row selections or de-selections result in this error. The item being referenced in the error is the one that was deleted. The problem is not present in single select mode.

Mar 09, 2015 5:31:58 PM com.vaadin.server.DefaultErrorHandler doDefault
java.lang.IllegalArgumentException: Given item id (vfc_C172/1425759773838) does not exist in the container
    at com.vaadin.ui.Grid$AbstractSelectionModel.checkItemIdExists(
    at com.vaadin.ui.Grid$AbstractSelectionModel.checkItemIdsExist(
    at com.vaadin.ui.Grid$MultiSelectionModel.setSelected(
    at com.vaadin.ui.Grid$
    at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.vaadin.server.ServerRpcManager.applyInvocation(
    at com.vaadin.server.ServerRpcManager.applyInvocation(
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(
    at com.vaadin.server.communication.PushHandler$
    at com.vaadin.server.communication.PushHandler.callWithUi(
    at com.vaadin.server.communication.PushHandler.access$200(
    at com.vaadin.server.communication.PushHandler$1.onRequest(
    at org.atmosphere.cpr.AsynchronousProcessor.action(
    at org.atmosphere.cpr.AsynchronousProcessor.suspended(
    at org.atmosphere.container.Servlet30CometSupport.service(
    at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(
    at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(
    at org.atmosphere.websocket.DefaultWebSocketProcessor$
    at org.atmosphere.util.VoidExecutorService.execute(
    at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(
    at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(
    at org.atmosphere.container.JSR356Endpoint$1.onMessage(
    at org.atmosphere.container.JSR356Endpoint$1.onMessage(
    at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(
    at org.apache.tomcat.websocket.WsFrameBase.processDataText(
    at org.apache.tomcat.websocket.WsFrameBase.processData(
    at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(
    at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(
    at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(
    at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$
    at org.apache.tomcat.util.threads.TaskThread$

Vaadin 7.4.1, Tomcat 8.0.20,

This looks a lot like the problem in
, which is still under work.

Johannes suggests a workaround there: “A workaround is to deselect removed items manually.”

It seems to help:

// Allow deleting the selected items
Button deleteSelected = new Button(“Delete Selected”, e → {
// TODO A workaround for #16195
List toDelete = new ArrayList();
for (Object itemId: grid.getSelectedRows()) {
grid.deselect(itemId); // Must deselect manually

// Delete all selected data items
for (Object itemId: toDelete)

[/code] Perhaps you can do the deselection in the same loop as the removal, I’m just playing safe there. However, I’m still getting some random IllegalStateExceptions about missing items occasionally. That could be a separate problem.

Thanks very much Marko. I missed #16195 and see now that Johannes is on it.

Is it safe to deselect(itemID) which changes the getSelectedRows collection while referring to that collection in the loop? Maybe it is, but this avoids any doubt and appears to work so far.

            final Collection<Object> itemIDsToBeRemoved = getSelectedRows();
                for ( Object itemID: itemIDsToBeRemoved ) {

I think it’s safe, as it looks like getSelectedRows() makes a copy of the selected items collection.

Therefore, you don’t need the variable there, but can just use for ( Object itemID: getSelectedRows()).

Moreover, it looks like the problem occurs next time the user changes the selection, so it should be perfectly fine to remove the selection
you remove the items from the container. Therefore, you could simply use the reset() on the selection model, which with multi-select empties the selection.

Button deleteSelected = new Button("Delete Selected", e -> {
    // Delete all selected data items
    for (Object itemId: grid.getSelectedRows())

    // TODO A workaround for #16195


Unfortunately the ticket (and a patch I wrote) has been left hanging due to design issues - in general it is very hard to efficiently ensure the selection is always in sync with the container contents. In AbstractSelect-based components (Table, ComboBox etc) the same issue was resolved by adding a “sanitizeSelection” method that can be manually invoked when needed. Those components do not suffer from the much more serious issue of actually throwing exceptions when the desync happens, and this problem should be fixed in Grid as soon as possible.

I just ran into this issue myself (using the default data source) and ended up here after searching for a solution. I wanted to mention as an FYI that the getSelectionModel().reset() workaround is working fine for me.

I used the ‘deselect’ workaround mentioned in #16195 which worked fine for me. Another issue occurs with multiselect and filters: add a filter to your grid, select a row, change the filter (first selected item not included in the result), and select another one…

java.lang.IllegalArgumentException: Given item id (7) does not exist in the container
    at com.vaadin.ui.Grid$AbstractSelectionModel.checkItemIdExists(
    at com.vaadin.ui.Grid$AbstractSelectionModel.checkItemIdsExist(
    at com.vaadin.ui.Grid$MultiSelectionModel.setSelected(
    at com.vaadin.ui.Grid$