VaadinServlet.getCurrent returns null

I have a Vaadin7 application I am trying to move to Vaadin12 and there is a line of code where I call VaadinServlet.getCurrent() to get the servlet ( I actually need to configuration information ). What is happening and how can I fix?

Obviously, this line of code works fine in Vaadin7.

In Vaadin 7, the internal ThreadLocal that was used for VaadinService.getCurrent() was “inheritable”, which means that when a new thread was created, the value was inherited from the parent thread. This was convenient in many cases, but it also caused memory leaks in association with thread pools. For this reason, Vaadin 8 and newer no longer allow the value to be inherited to child threads.

To fix the problem in your application, you need to retrieve the VaadinService reference already at the point when the background job is started. You can then explicitly pass it to the job, e.g. as a constructor parameter or as an (effectively) final variable that is used in the closure.

Actually, I just had to change import com.vaadin.server.VaadinServlet; to import com.vaadin.flow.server.VaadinServlet;. There is a addon we use that seems to reference vaadin 7 stuff, and thus neither of us noticed the wrong import statement. Either we should have stopped using this addon or updated it to the latest version.

Lesson to learn from this: make sure you are no referencing Vaadin 7 via depencies in your pom, I guess.