0

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.

Rafa Romero
  • 2,667
  • 5
  • 25
  • 51
  • but when u setting that attribute? – Kishan Bheemajiyani Jul 16 '14 at 13:43
  • @Rafa, that NPE has to be coming from somewhere else. Per the [Java Servlet API](http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpSession.html#getAttribute(java.lang.String), `getAttribute` will return NULL if no value is bound to the provided key, but it will never throw a NullPointerException itself. – sherb Jul 17 '14 at 00:13

1 Answers1

2

Have you tried to check for a null value?

if(session.getAttribute("socialLogin") != null) {
   boolean socialLogin = (Boolean) session.getAttribute("socialLogin");
}
Stefan
  • 12,108
  • 5
  • 47
  • 66