0

Hey, I'm getting so fustrated with this now!! I'm making a site in jsp and I have created the registration page and the login page and everything works fine. However I want to start to use the session login to personalise the page depending if they have logged in or not. I thought I had done this using the following code as it worked fine once you had logged in but if you went to the page without logging in it throws a internal server error print stack trace error 500 with a NullPointerException.

<% if (sess.getAttribute("loggedUser").toString() == null) { %>
         <jsp:include page="\LoggedSessionMenu.jsp" />
      <% } else { %>
         <jsp:include page="NonSessionMenu.jsp" />
      <% } %>

I'm guessing its because the session is empty but I tried with an if statement to test if it is null and this didn't work either.
Any help would be awesome as I'm going mad trying to fix this!
Cheers
Doops

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by jwenting
0

The string you get back from the session is null, so your attempt to call toString() on it results in an NPE.
You should really use JSTL as well.
Something like

<c:choose>
   <c:when test="not empty session.loggedUser">
  ...
   </c:when>
   <c:otherwise>
  ...
   </c:otherwise>
</c:choose>

Makes it immediately apparent what you're trying to do, and will not result in an NPE.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.