• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

request.setAttribute() nullpointer in struts 2

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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";
try{
request.setAttribute("disp",hai);
}
catch(Exception e){
System.out.println("Exception occured :"+e);
}
return SUCCESS;
}

am getting null pointer exception



 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: http://struts.apache.org/2.1.2/struts2-core/apidocs/org/apache/struts2/interceptor/RequestAware.html

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic