I get an NPE when I try to show a Notification from a background thread if I update another component at the same time.
In the simple code below, if I remove the line
myLabel.setText("New label text");
it works fine.
@BodySize(height = "100vh", width = "100vw")
@HtmlImport("styles/shared-styles.html")
@Route("")
@Theme(Lumo.class)
@Push(PushMode.AUTOMATIC)
public class MainView extends VerticalLayout implements RouterLayout {
private static final long serialVersionUID = 1889638595309516934L;
public MainView() {
Label myLabel = new Label("It's a label");
Button button = new Button("Click me", event -> {
final UI currentUI = UI.getCurrent();
new Thread(() -> {
try {
Thread.sleep(2000);
currentUI.access(() -> {
myLabel.setText("New label text"); // if I remove this line, it works.
Notification.show("Button was clicked !", 5000, Position.BOTTOM_END);
});
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}).start();
});
add(myLabel, button);
}
}
Caused by: java.lang.NullPointerException
at com.vaadin.flow.server.ServletHelper.getContextRootRelativePath(ServletHelper.java:188)
at com.vaadin.flow.server.BootstrapHandler.getApplicationParameters(BootstrapHandler.java:913)
at com.vaadin.flow.server.BootstrapHandler.access$000(BootstrapHandler.java:82)
at com.vaadin.flow.server.BootstrapHandler$BootstrapUriResolver.getContextRootUrl(BootstrapHandler.java:344)
at com.vaadin.flow.shared.VaadinUriResolver.processContextProtocol(VaadinUriResolver.java:89)
at com.vaadin.flow.shared.VaadinUriResolver.resolveVaadinUri(VaadinUriResolver.java:60)
at com.vaadin.flow.server.VaadinServlet.resolveUrl(VaadinServlet.java:712)
at com.vaadin.flow.server.VaadinServlet.resourceIsFound(VaadinServlet.java:689)
at com.vaadin.flow.server.VaadinServlet.computeUrlTranslation(VaadinServlet.java:681)
at com.vaadin.flow.server.VaadinServlet.getUrlTranslation(VaadinServlet.java:633)
at com.vaadin.flow.component.internal.UIInternals.getHtmlImportValue(UIInternals.java:813)
at com.vaadin.flow.component.internal.UIInternals.lambda$addHtmlImport$3(UIInternals.java:807)
at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1540)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at com.vaadin.flow.component.internal.UIInternals.addHtmlImport(UIInternals.java:807)
at com.vaadin.flow.component.internal.UIInternals.lambda$addComponentDependencies$0(UIInternals.java:798)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at com.vaadin.flow.component.internal.UIInternals.addComponentDependencies(UIInternals.java:798)
at java.lang.Iterable.forEach(Iterable.java:75)
at com.vaadin.flow.server.communication.UidlWriter.encodeChanges(UidlWriter.java:340)
at com.vaadin.flow.server.communication.UidlWriter.createUidl(UidlWriter.java:139)
at com.vaadin.flow.server.communication.AtmospherePushConnection.push(AtmospherePushConnection.java:190)
... 9 more
Any idea ?