Error with UI.getCurrent().getPage().setTitle("Name");

UI.getCurrent() returns null.

When I use UI.getCurrent().getPage().setTitle(“Name”) at beginning of the code I got next error (pretty sure that it was worked in prev versions, now 18.0.5):


15:34:57.089 ERROR 
java.lang.IllegalArgumentException: Unable to create an instance of 'com.bidocflow.Okinawa'. The constructor threw an exception.
	at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:514) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.internal.ReflectTools.createInstance(ReflectTools.java:451) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.di.DefaultInstantiator.create(DefaultInstantiator.java:137) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.di.DefaultInstantiator.getOrCreate(DefaultInstantiator.java:66) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.server.AppShellRegistry.modifyIndexHtml(AppShellRegistry.java:235) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.server.communication.IndexHtmlRequestHandler.synchronizedHandleRequest(IndexHtmlRequestHandler.java:109) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1539) ~[flow-server-5.0.2.jar:5.0.2]

	at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:238) ~[flow-server-5.0.2.jar:5.0.2]

	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[javax.servlet-api-3.1.0.jar:3.1.0]

	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791) ~[jetty-servlet-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550) ~[jetty-servlet-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602) ~[jetty-security-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1435) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501) ~[jetty-servlet-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1350) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:191) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.Server.handle(Server.java:516) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388) ~[jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633) [jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380) [jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:273) [jetty-server-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [jetty-io-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [jetty-io-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [jetty-io-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:773) [jetty-util-9.4.36.v20210114.jar:9.4.36.v20210114]

	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:905) [jetty-util-9.4.36.v20210114.jar:9.4.36.v20210114]

	at java.lang.Thread.run(Thread.java:832) [?:?]

Caused by: java.lang.reflect.InvocationTargetException
	at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]

	at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) ~[?:?]

	at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]

	at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[?:?]

	at java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[?:?]

	at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:483) ~[flow-server-5.0.2.jar:5.0.2]

	... 38 more
Caused by: java.lang.NullPointerException: Cannot invoke "com.vaadin.flow.component.UI.getPage()" because the return value of "com.vaadin.flow.component.UI.getCurrent()" is null
	at com.bidocflow.Okinawa.<init>(Okinawa.java:76) ~[classes/:?]

	at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]

	at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) ~[?:?]

	at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]

	at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[?:?]

	at java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[?:?]

	at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:483) ~[flow-server-5.0.2.jar:5.0.2]

	... 38 more
[WARNING]
 /
javax.servlet.ServletException: javax.servlet.ServletException: com.vaadin.flow.server.ServiceException: java.lang.IllegalArgumentException: Unable to create an instance of 'com.bidocflow.Okinawa'. The constructor threw an exception.
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:162)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
	at org.eclipse.jetty.server.Server.handle(Server.java:516)
	at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388)
	at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:273)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
	at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:773)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:905)
	at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: javax.servlet.ServletException: com.vaadin.flow.server.ServiceException: java.lang.IllegalArgumentException: Unable to create an instance of 'com.bidocflow.Okinawa'. The constructor threw an exception.
	at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:240)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1435)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1350)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:191)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
	... 12 more
Caused by: com.vaadin.flow.server.ServiceException: java.lang.IllegalArgumentException: Unable to create an instance of 'com.bidocflow.Okinawa'. The constructor threw an exception.
	at com.vaadin.flow.server.VaadinService.handleExceptionDuringRequest(VaadinService.java:1588)
	at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1551)
	at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:238)
	... 30 more
Caused by: java.lang.IllegalArgumentException: Unable to create an instance of 'com.bidocflow.Okinawa'. The constructor threw an exception.
	at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:514)
	at com.vaadin.flow.internal.ReflectTools.createInstance(ReflectTools.java:451)
	at com.vaadin.flow.di.DefaultInstantiator.create(DefaultInstantiator.java:137)
	at com.vaadin.flow.di.DefaultInstantiator.getOrCreate(DefaultInstantiator.java:66)
	at com.vaadin.flow.server.AppShellRegistry.modifyIndexHtml(AppShellRegistry.java:235)
	at com.vaadin.flow.server.communication.IndexHtmlRequestHandler.synchronizedHandleRequest(IndexHtmlRequestHandler.java:109)
	at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
	at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1539)
	... 31 more
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
	at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:483)
	... 38 more
Caused by: java.lang.NullPointerException: Cannot invoke "com.vaadin.flow.component.UI.getPage()" because the return value of "com.vaadin.flow.component.UI.getCurrent()" is null
	at com.bidocflow.Okinawa.<init>(Okinawa.java:76)
	... 44 more

It’s hard to say anything without more context, but generally speaking, if you want to be certain that a UI is available, you can override the onAttach method and use the UI instance from the event there:

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        super.onAttach(attachEvent);
        UI ui = attachEvent.getUI();
        // do something with ui
    }