views:

32

answers:

2

Hi,

I am facing a strange error in Struts2, I am getting a null pointer exception when trying to set attribute into request scope,

Struts Problem Report

Struts has detected an unhandled exception: Messages: File: org/apache/catalina/connector/Request.java Line number: 1,424

Stacktraces
java.lang.NullPointerException
    org.apache.catalina.connector.Request.setAttribute(Request.java:1424)
    org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:503)
    javax.servlet.ServletRequestWrapper.setAttribute(ServletRequestWrapper.java:284)
    com.inrev.bm.action.IRBrandMgmtAction.wrdTwts(IRBrandMgmtAction.java:81)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

My code is given below,

public class IRBrandMgmtAction extends ActionSupport implements SessionAware,ServletRequestAware {


private static final long serialVersionUID = 1L;

private Map session;

private HttpServletRequest request; 

private IRWordToTrack wrdtotrack;

private IRBrandMgmtDAO brandMgmtDAO;

private static org.apache.log4j.Logger log = Logger.getLogger(IRBrandMgmtAction.class);

and the method where this is happening,

public String wrdTwts()
{
    ArrayList<IRBrandTrackBean> messages = null;
    IRDateUtil dtUtil = new IRDateUtil();
    String wordId = request.getParameter("wordId");

    IRUser user = (IRUser) session.get("user");
    IRWordToTrack word = brandMgmtDAO.getWord(Integer.parseInt(wordId));

    if(word!=null)
    {
        messages = brandMgmtDAO.viewMsgForUser(word.getWord().toUpperCase().trim(), null, dtUtil.getTimeZoneOffset(user.getTimeZone()));
    }

    request.setAttribute("messages",messages);

    return "tweets";
}

I am using tomcat 6 server.

Regards, Rohit

A: 

It's difficult to know the cause of your problem, but three suggestions come to mind:

  • Perhaps it's a Tomcat bug, upgrade to the most recent version.
  • Make sure the objects you put in Request/Session contexts are small beans, with no references to other objects.
  • Your code has several smells. In Struts2, you should (almost) never access the HttpRequest object (and rarely the session). The point of Struts2 is decoupling (almost) completely your action from the Servlet layer; this eases testing and in general keeps you happy. Your action method should normally read its own fields (typically already set from the http request by the "param interceptor"), talk to the service layer, set some other own fields and return a result. To set an httprequest inside a Struts2 method is almost always a bad practice.
leonbloy
Thanks Leonbloy,Am actually new to struts2, have been using struts1.2 untill this project, thanks for all your suggestions. Just a few questions thought,* What would you suggest would be the best way to pass values in strust2, defining getter setters?* This error of request object null doesnt seem to be happening when I use this HttpServletRequest request = ServletActionContext.getRequest();Can you throw some light?
rohitgu
A: 

It might be an idea to make your action class RequestAware instead of ServletRequestAware.

In your current code are you sure that your setServletRequest method is being called? Have you defined any custom interceptors which cause the default interceptors to be discarded? In the case of Session/Request aware we're interested in the ServletConfigInterceptor.

Alex