IllegalStateException Can't move a node from one state tree to another.

If you have been bitten by Vaadin’s IllegalStateException: Can't move a node from one state tree to another. If this is intentional, first remove the node from its current state tree by calling removeFromTree, read on.

The problem is that a component, which is already attached to one UI, is being inserted into another UI as well. This usually happens when:

  • using DI scopes in a bad way on a component, e.g. SessionScoped or Singleton scoped component - a very bad idea. For example, you need to add a scope to a Spring component like @UIScope or @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE). If you don’t add any scope then it’s a singleton = the same component is shared for all the views/users and it leads to this stacktrace.
  • placing a component (e.g. VaadinIcon) into a static field (or into an enum).

Moving a component from one UI to another will cause the component to disappear from the original UI for no apparent reasons, causing a lot of headscratching for the developer, and therefore this is definitely not a good practice.

A Vaadin ticket 9376 discusses the problem in deeper detail.

What you need to do is:

  • Figure which component is causing this issue (is moved to another UI)
  • Fix scopes for that component.

You can figure out the offending component using debugger; you can even track where the component was created and inserted.

In the future, Vaadin will print this information in the exception message itself.

Written on November 22, 2023