title: How can i access any data in session inside spring aop logger class in java?
body: <p>I am new to spring AOP and I am trying to implement logging for my Action class.Now i also want to save certain information in the DataBase while logging.For this to work I have certain data in my session that i need to access in my logger class but trying to access it, a "Null pointer exception occurs". Any help would be much appreciated...Thanx</p>
  
  <p>LoggingInterceptor.java</p>
  
  <pre><code>package com.mcmc.utility;
  
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.Writer;
  import java.lang.reflect.Method;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.servlet.http.HttpSession;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.struts2.interceptor.SessionAware;
  import org.springframework.aop.AfterReturningAdvice;
  import org.springframework.aop.MethodBeforeAdvice;
  import org.springframework.aop.ThrowsAdvice;
  
  import com.mcmc.hn.bean.UserInfo;
  import com.mcmc.hn.dao.interfaces.UserManagementDao;
  
  public class LoggingInterceptor implements MethodBeforeAdvice,SessionAware{ //, AfterReturningAdvice, ThrowsAdvice
      private static Log log = null;
      Map&lt;String, Object&gt; sesionMap=null;
      HttpSession session = null;
      UserManagementDao userManagementDao;
  
  
  public LoggingInterceptor(){
  }
  
  public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
      log = LogFactory.getLog(arg2.getClass());
      log.info("Beginning method: "+arg0.getName());
      System.out.println("BEFORE&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Beginning method: "+arg0.getName());
  
      HashMap loggingDescription = new HashMap(); 
      loggingDescription.put(new Integer(1),"This is a method to display List of Users");
      loggingDescription.put(new Integer(2),"This is a method to display account Information of the logged-in User");
  
       UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT); &lt;-- THIS IS WHERE NULL POINTER EXCEPTION IS GENERATED.
      String usrName = user.getFname() + " " + user.getLname();
      String usrId = user.getUser_id();
      String method="";
      if(arg0.getName().equals("displayUser")){
          method = (String) loggingDescription.get(1);
      }else{
          method = (String) loggingDescription.get(2);
      }
      userManagementDao.logInfo(method,usrName,usrId);
  }
  
  public void setSession(Map&lt;String, Object&gt; map) {
      this.sesionMap = map;
  }
  }        
  </code></pre>
  
  <p>applicationContext.xml---> The code for property</p>
  
  <pre><code>&lt;!-- Bean configuration --&gt;
  &lt;bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean" &gt;
      &lt;property name="proxyInterfaces" value="com.mcmc.hn.dao.interfaces.UserManagementDao"&gt;
      &lt;/property&gt;
      &lt;property name="target"&gt;
          &lt;ref local="userManagementDao" /&gt;
      &lt;/property&gt;
      &lt;property name="interceptorNames"&gt;
          &lt;list&gt;
              &lt;value&gt;theTracingBeforeAdvisor&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt; 
  
      &lt;!-- Bean Classes --&gt;
  
  &lt;!-- &lt;bean id="userManagementDao" class="com.mcmc.hn.dao.UserManagementDaoImpl" /&gt; --&gt;
  &lt;!-- Advisor pointcut definition for before advice --&gt;
  &lt;bean id="theTracingBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&gt;
      &lt;property name="advice"&gt;
          &lt;ref local="theTracingBeforeAdvice" /&gt;
      &lt;/property&gt;
      &lt;property name="pattern"&gt;
          &lt;value&gt;.*displayUser.*&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Advisor pointcut definition for after advice --&gt;
  
  &lt;!-- Advice classes --&gt;
  &lt;bean id="theTracingBeforeAdvice" class="com.mcmc.utility.LoggingInterceptor" /&gt;
  </code></pre>
  
  <p>UserManagementAction.java--->Action Class</p>
  
  <pre><code>public class UserManagementAction extends ActionSupport implements ModelDriven&lt;UserInfo&gt;, RequestAware,SessionAware, ServletResponseAware, ServletRequestAware {
  
  
  private UserInfo userInfo = new UserInfo();
  private UserAddress userAddress = new UserAddress();
  
  UserManagementDao userManagementDao;
  KeywordCategoryDao keywordCategoryDao;
  
  HttpServletRequest request = null;  
  HttpServletResponse response = null;
  HttpSession session = null;
  /*ProxyFactoryBean proxyBean = null;*/
  
  Map&lt;Long, Object&gt; userSessionMap=new HashMap&lt;Long, Object&gt;();  // Map is used to hold the user session reference in Servlet Context 
  
  String userName="";
  String password="";
  String remStatus="";
  
  Map&lt;String, Object&gt; reqMap=null;
  Map&lt;String, Object&gt; sesionMap=null;
  
  String usrName="";              //Variables for logging 
  String usrId="";
  
  
  
  /**
   * Calls a function to retrieve the values from Database. 
   * @return SUCCESS in oder to load the JSP page.
   */
  @SuppressWarnings({ "unchecked" })
  
  public String displayUser(){
  
      UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT);
      usrName = user.getFname() + " " + user.getLname();
      usrId = user.getUser_id();
      String method="This is a method to display List of Users";
      userManagementDao.logInfo(method,usrName,usrId);
  
      ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:../../WEB-INF/applicationContext.xml");
      UserManagementDao userManagementDao=(UserManagementDao) appContext.getBean("proxyBean");
      List&lt;UserInfo&gt;  Lst  = userManagementDao.displayUser();
      reqMap.put("userList", Lst);
  
      return SUCCESS;
  }
  
      public String login(){
      ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:../../WEB-INF/applicationContext.xml");
      UserManagementDao userManagementDao=(UserManagementDao) appContext.getBean("proxyBean");
      String returnStatus = SUCCESS;
      session = request.getSession();
      long usr_id;
      long roleId;
      int count=0,flag=0;
      Map&lt;Long, Object&gt; userMap=new HashMap&lt;Long, Object&gt;();
  
      int status=0;
      if(remStatus.equals("on")){
          status=1;
      }else{
          status=0;
      }
      session.setAttribute("status", status);
  
      UserInfo user=userManagementDao.login(userName, password);
      userMap=(Map&lt;Long, Object&gt;)session.getServletContext().getAttribute(MCMCConstants.USER_SESSION_REFERENCE_MAP); //Retrieving userMap from Servlet Context with User Session Info
  
      if( userMap!= null){
          if(userMap.containsKey(user.getId())){
              flag=1;
          }
      }
  
      if(user!=null &amp;&amp; flag==0 ){
          count=1;
          sesionMap.put(MCMCConstants.USER_INFO_OBJECT, user);&lt;--THIS IS THE DATA THAT IS IN SESSION THAT I NEED TO ACCESS IN LOGGINGINTERCEPTOR.java
          usr_id=user.getId();
          roleId=user.getRoleId();            
          //System.out.println("USERID::::::"+roleId);
          sesionMap.put(MCMCConstants.USER_INFO_ID,usr_id);
          sesionMap.put(MCMCConstants.USER_ROLE_ID,roleId);
          // Loading Constants into Session
          SessionUtility.loadCategoryList(sesionMap, keywordCategoryDao);
          if(status==1){
              Cookie cookie = new Cookie ("loginData",userName + "|" + password+"?" + status);
              response.addCookie(cookie);
          }
  
          userSessionMap.put(user.getId(), user.getUser_id());
          session.getServletContext().setAttribute(MCMCConstants.USER_SESSION_REFERENCE_MAP, userSessionMap); //Setting userSessionMap to ServletContext
  
          returnStatus=SUCCESS;
      }else{
          returnStatus= "input";
      }
  
      return returnStatus;
  }
  
  /* Getter And Setter Methods as required */
  }
  </code></pre>