When Component.getUI() returns null?

With vaadin7 beta3 it happens to me that even components the are just displayed in the browser return
null
when I invoke
Component.getUI()
. To circumvent the problem I use
UI.getCurrent()
when
Component.getUI() == null
.

But I would like to understand this: Why does
Component.getUI()
sometimes returns
null
even if the component is just displayed?

Beats me. Component.getUI should return the UI that it is attached to - unless there is some problem with the component hierarchy, e.g. if some parent component has a broken implementation of setParent or addComponent.

To find out where the hierarchy is broken, I would suggest recursing through the component’s parents until you get null or reach the UI.

I always use UI.getCurrent(), as it is safer to use when you don’t have to think about if the component is attached or not yet. Also you can optimize that a bit by adding MyUI.get() that does the casting for you, so you get a MyUI directly, instead of UI.