Rafa Romero
Rafa Romero

Reputation: 2716

Getting and NullPointerException while accessing to a session attribute

I am trying to access to a session attribute from a Filter in my webapp running on Tomcat:

boolean socialLogin = (Boolean) session.getAttribute("socialLogin");

I'm getting a NullPointerException, how can I handle the situation of if the attribute exists or not without having to catch a NPE?

The sessión is not null, I have checked it before.

Upvotes: 0

Views: 1227

Answers (1)

Stefan
Stefan

Reputation: 12453

Have you tried to check for a null value?

if(session.getAttribute("socialLogin") != null) {
   boolean socialLogin = (Boolean) session.getAttribute("socialLogin");
}

Upvotes: 2

Related Questions