-1

I have created a CommonServiceImpl, it is responsible for removal of session in spring boot project. But I get a Potential null pointer access error by getAoccur please help me get rid of this error.

@Service public class CommonServiceImpl implements CommonService { 
   HttpSession session=((HttpServletRequest)RequestContextHolder.getRequestAttributes()).getSession();

   if (session != null) {
       // Remove the success and error message attributes from the session
       session.removeAttribute("succMsg");
       session.removeAttribute("errorMsg");
   }
}

The Potential null pointer access: The method getRequestAttributes() may return nullJava. How can I rid of this issue?

1 Answer 1

0

Just check if the request object is null, and return early if it is.

HttpServletRequest request = (HttpServletRequest)RequestContextHolder.getRequestAttributes();

if (request == null) {
    return;
}

HttpSession session = request.getSession();

if (session != null) {
   // Remove the success and error message attributes from the session
   session.removeAttribute("succMsg");
   session.removeAttribute("errorMsg");
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.