UI.getCurrent() returning null

Hi,

I’m experimenting a strange behavior in my Vaadin application

public class BaseUI extends UI implements javax.jms.MessageListener{
//Code for initialization

public synchronized void onMessage(Message message){
ObjectMessage message = (ObjectMessage)message;
UI currentUI = UI.getCurrent(); → returns NPE
//other code
}
}

Why does UI.getCurrent() in my onMessage method always returns a null UI?
The UI has been already instantiated before, because this method pushes a JMS notification on the ui (chat-style), so I cannot figure why this method returns null in this case.

Can anyone help me, please?

Hi Antlia, you are not processing a request so the current UI has not been defined.


UI
: “The current UI is automatically defined when processing requests to the server. In other cases, (e.g. from background threads), the current UI is not automatically defined.”

If you need a reference to your BaseUI in onMessage, and your BaseUI is the MessageListener implementor. Use “this” to get a reference to your current ui. I mean: UI currentUI = this
.

Many thanks for the clear explanation omar!