1

I am making a Struts2 application.

I am trying to logout, but I am getting java.lang.NullPointerException

My method is:

  public String logout(){
    
    Map session = ActionContext.getContext().getSession();
    SessionMap sessionMap=(SessionMap)session;
    log.info("session:"+session);
    log.info("sessionmap:"+sessionMap);
    log.info("sessionMap.get()"+sessionMap.get("USROBJECT"));
    try{
    if(sessionMap!=null){
        
        sessionMap.remove("USROBJECT");
        sessionMap.invalidate();
        
    }
    }
    catch(Exception e){
        log.info("Exception is:"+e.toString());
    }
        
    
    return "LOGOUT";
}

I am getting the object when I am trying to print session , sessionMap and "USROBJECT" object, but when I am calling remove(), I am getting an exception.

login method:

  public Map session = ActionContext.getContext().getSession();
  public SessionMap sessionMap;   
  public void setSession(Map session) {
    sessionMap=(SessionMap) session;
    }
     
  public String login(){
       us = new UserSession();
       us= getUserDetails();
       log.info("userName is:"+us.getUserName());
       log.info("password is:"+us.getPassword());
       us.setLastLoginTime(loginTime);
    
       sessionMap.put("USROBJECT", us);
       return "register";
    }

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" /> 
 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <package name="Hello" extends="struts-default">

   <action name="*LoginAction" method="{1}"
        class="com.pgnext.apps.action.LoginAction">




    <result name="register">/jsp/register.jsp</result> 
    <result name="error">/jsp/error.jsp</result>
    <result name="logout">/jsp/login.jsp</result> 
  </action>

  </package>
</struts>
  17:09:31,038  INFO LoginAction:171 - going to remove USROBJECT
  17:09:31,038  INFO LoginAction:183 - Exception is : java.lang.NullPointerException

    
    
Roman C
  • 49,761
  • 33
  • 66
  • 176
Raina
  • 45
  • 4

2 Answers2

0

You don't need to invalidate the session (a session is usually created when you land on a JSP), just remove your object from it:

private final static String USER_KEY = "USROBJECT";

public String logout(){
    Map<String,Object> session = ActionContext.getContext().getSession();
    if (session.containsKey(USER_KEY)) { 
        session.remove(USER_KEY);
    }
    return "LOGOUT";
}

Also consider

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I had added the"USROBJECT" in sessionMap, but when I am trying to remove it, I am getting null pointer exception – Raina Mar 15 '16 at 11:09
  • Retry with the above code. Don't use SessionMap, and use constants to ensure you're not genearting typos in the key name. Also try retrieving the session in the way described in the answer linked. P.S: remember you can also up-vote now, since you have 15 reputation points. – Andrea Ligios Mar 15 '16 at 11:15
0

I think the problem you have with session invalidation that you are trying reuse the session object after the session is invalidated. I have explained it here.

If you want to access the struts session after you invalidated the servlet session you should update or renew the struts session.

On the JSP page you can check the object with s:if tag

<s:if test="#session.USROBJECT != null">
  ....
</s:if>

After session invalidation you should return a result of type redirect or redirectAction.

<result type="redirect" name="logout">/jsp/login.jsp</result> 

The login.jsp should not be a welcome-file listed in the web.xml.

And in login() action you didn't initialize a sessionMap.

public String login(){
   Map session = ActionContext.getContext().getSession();
   SessionMap sessionMap=(SessionMap)session;
   UserSession us = getUserDetails();
   if (us != null) {
     log.info("userName is:"+us.getUserName());
     log.info("password is:"+us.getPassword());
     us.setLastLoginTime(loginTime);

     sessionMap.put("USROBJECT", us);
   }
   return "register";
}

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Hi, even after adding your code, I am getting the same exception. I am not able to understand when i am able to print the user object, then why am I getting null pointer exception when I am trying to remove it – Raina Mar 15 '16 at 11:37
  • The exception stacktrace is needed to diagnose the code. Also it would be good if you add `struts.xml` to the question. – Roman C Mar 15 '16 at 11:39
  • I am adding my login method, logout method , struts.xml, and jsp..please refer. – Raina Mar 15 '16 at 11:49
  • Try the redirect result. – Roman C Mar 15 '16 at 12:04
  • I have initialized my sessionMap as public Map session = ActionContext.getContext().getSession(); public SessionMap sessionMap; public void setSession(Map session) { sessionMap=(SessionMap) session; } – Raina Mar 16 '16 at 05:41