UI.access throws always null exception


I have a problem about the notification using UI.access, i want to notify the user when there is a new event arrived.
But by using access, i get always an exception “java.lang.NullPointerException”.
MainUI.getCurrent() is a static method. Can you help me please?

I use this method to ask the view to display a notification:

In Presenter.java

view.showNotification(property.getEvent().toString(), "Alarm changed", Type.HUMANIZED_MESSAGE);

In View.java

    public void showNotification(String caption, String description, Type type)
        MainUI.getCurrent().access(new Runnable() {
            public void run() {

In MainUI.java:

    public static MainUI getCurrent()
        return (MainUI) UI.getCurrent();

Using access() is required when accessing a UI from outside it. Therefore, the UI.getCurrent() will
return something wrong – either null, when you’re calling it from a non-UI thread, or wrong UI class, when you’re trying to access the UI from another UI (casting which to MainUI would result in null if the class is different).

The “current” really means current UI, not the target UI which you are accessing. If you were accessing the current one, you wouldn’t need the access() in the first place…

Thanks you for your reply, can you tell me how can i display a message to the user when there is a new event arrived?
And how can i detect whether the user is on the current alarm view page?

Your question is rather broad, as it depends a lot on what you mean by new event. Does it come from other users (from their UIs), some background thread, different servlets, or what?

Generally speaking, the messages probably come from outside the UI, so in that sense access() is needed. So, you need a way to get the UI instances to which to send i. In the book section on push there’s an example about
broadcasting such messages to all open UIs
. But if your “alarms” are directed to specific users, so you’d need different addressing. You probably need a UI registration mechanism, as is done in the example.

Detecting whether a user is on some alarm view page entirely depends on your application, how you manage your views.