0

I'm getting a NullPointerException while accessing the application map that should've been injected in the Interceptor since I've implemented the ApplicationAware interface.

public String intercept(ActionInvocation ai) throws Exception {
    String result = "expire";
    app.put("login_msg", "Session Expired");
    Map session = ai.getInvocationContext().getSession();

    try {
        if ((session != null && session.get("login").toString().equalsIgnoreCase("true")) || !app.get("db_name").toString().equals("")) {
        log.info("login hai");
        app.put("login_msg", "");
        result = ai.invoke();
        } else {
            log.error(" LOGIN EXPIRED ");
        }
    } catch (NullPointerException npe) {
        log.error("Error : " + npe);
    }

    log.info("Interceptor Result : " + result);
    return result;
}

@Override
public void setApplication(Map<String, Object> map) {
    app = map;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    That you're (trying to) putting a string that explicitly says "session", as in login session, into something explicitly called "application" should be a huge red warning flag that something may have gone wrong in the thought process. – Dave Newton Jul 25 '16 at 13:09
  • @HarshdeepSingh This is already another question. – Roman C Jul 25 '16 at 19:23

1 Answers1

2

ApplicationAware is an interface used to inject a global map into actions, not interceptors:

Actions that want to be aware of the application Map object should implement this interface. This will give them access to a Map where they can put objects that should be available to other parts of the application.

There are mutliple ways to achieve what you want, but I'm pretty sure it's not what you need. Why on earth are you putting login informations into a global objects ? Just write to and read from the Session, that's what you need.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Do you still have doubts @harshdeep ? – Andrea Ligios Jul 25 '16 at 13:54
  • Thanks for this buddy! I've used session only, but still unable to print message (that is been set in interceptor) to the jsp page where it is been redirect. Actually this is to check login session and if session is expired it will redirect it to login page. I want to print "Session Expired" message on that page. Please help. – Harshdeep Singh Jul 26 '16 at 05:43
  • Sounds like a new question, BTW `` will print the `foo` value. – Andrea Ligios Jul 26 '16 at 08:31
  • Umm...this interceptor it for checking weather session is been destroyed or not and if session is destroyed then it will redirect to login page. As session is expired, #session variable will be null. – Harshdeep Singh Jul 26 '16 at 13:02
  • No. [#session is a Map created by Struts2 over the underlying HttpSession.getAttributes](http://stackoverflow.com/a/19540712/1654265), and it will never be null. It will contain the value if the user logged in successfully and the session is not expired, it will not contain the value otherwise. – Andrea Ligios Jul 26 '16 at 13:23
  • Also read this (with the question too) http://stackoverflow.com/a/27702918/1654265 – Andrea Ligios Jul 26 '16 at 13:37