Search code examples
tomcatnullpointerexceptionhttpsession

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.


Solution

  • Have you tried to check for a null value?

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