Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
getApplication() from component returns null
Hi there,
My question is rather simple.
I have one Application class (eg: MyApp) and one CustomComponent class (eg: MyComp).
In MyApp I have
MyComp comp = new MyComp();
content.addComponents(comp);
...
public void myAppMethod1(){
....
}
in MyComp, I want to call a method in MyApp, so I wrote:
((MyApp)this.getApplication()).myAppMethod1();
However it throw null exception for getApplication().
Any idea? I am using the same way as in C#, from user control, i can use this.parentForm.method().
Br,
Xuan
Hi all,
No need to continue the discussion, I found the reason. In C# the onload event is fired when the control is attached (rendered). however in Vaadin the layout is rendered in class constractor (buildMainLayout), so the Application is empty at that time.
BTW, is there any EASY way to render the screen only after the component is attached(added)?
BR,
Xuan
You are probably calling getApplication() from the constructor of the component, before the component is actually attached to the application. It only becomes attached to the application when added to the main window or to a component attached to the main window.
Please see the the documentation of getApplication().
The usual solutions are either to provide the relevant references for the contructor as parameters or move the logic to the attach() method.