File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes request.setAttribute() nullpointer in struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "request.setAttribute() nullpointer in struts 2" Watch "request.setAttribute() nullpointer in struts 2" New topic

request.setAttribute() nullpointer in struts 2

Deepu Surendran

Joined: Feb 07, 2013
Posts: 2
I have created

HttpServletRequest request=null; in Action Class

and trying to send a object using request.setAttribute in a method()

eg:public String listCourses(){

String hai="Test";
catch(Exception e){
System.out.println("Exception occured :"+e);
return SUCCESS;

am getting null pointer exception

Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Obviously the request variable's value is null since it has not been set to anything. Actions in Struts2 are not servlets.

In order to access the servlet request, your action needs to implement the RequestAware interface. Please refer to the following API documentation:

In order to implement this interface:
1) Your action class must declare that it implements the interface
2) Include the setter method (and optionally the getter) in your class:
public void setRequest(Map request) {
this.request = request;

public Map getRequest() {
return request;
3) Declare an instance request variable of type Map

Since the request is a Map, you use Map's put method to set request attributes.
I agree. Here's the link:
subject: request.setAttribute() nullpointer in struts 2
It's not a secret anymore!