application null

Hi ,

i have a login form which inherits com.vaadin.Application , [this is the main application]

this user info is used through out .

i am storing this user info in httpsession.

but once the user is logged in , i cannot acess the httpsession from another page .

below is the code .

Code for setting the http session

"WebApplicationContext webCtx = (WebApplicationContext)getApplication().getContext();
HttpSession session = webCtx.getHttpSession();
session.setAttribute(“user”,event.getLoginParameter(“username”));

"

code for getting the session in another class which interits VeticalLayout.

“HttpSession session = ((WebApplicationContext)getApplication().getContext()).getHttpSession();”

A NullPointerException is coming in the getApplication() method , ie the application object is null.

How can i get the application object in all other screens [which inherits VerticalLayout]


Regards,
Niyas

Hi All,

Please provide a solution to this problem.


Regards,
Niyas

If I remember correctly, getApplication() will return null if the component isn’t attached to the application. To get the application instance from anywhere in the application, I suggest you use the ThreadLocal pattern. See
this wiki page
for more details.

Actually, l suggest you use InheritedThreadLocal instead; same idea, really, but InheritedThreadLocal allows a forked thread to recover the variables. This is necessary if you have any long running processint.

Yeah, the wiki article says as much.

Do you know if there is any downside for using InheritableThreadLocal over ThreadLocal, or should one always use the former?

I am not aware of any downside. For me it is just defensive programming – it is really easy to get caught with ThreadLocal because the get() method is visible to the thread code, but returns an unexpected null.

Thanks everybody … it works …

Now i can access the application object across screens.


Regards,
Niyas