• 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

Adding to vector

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Im getting this exception while executing my struts application.
Here is my ActionServlet

public class UserAction extends Action{

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
System.out.println("In execute method of UserAction");
Vector users = (Vector) (request
.getSession()
.getServletContext()
.getAttribute("users"));

UserForm uf = new UserForm();
uf=(UserForm)form;

System.out.println("In execute method of UserAction printing First name");
System.out.println(uf.getFirstName());

users.add(uf);

request.getSession().getServletContext().setAttribute("users", users);
return (mapping.findForward("registration"));
}
}







Error
---------

javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1480)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:524)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.NullPointerException
examples.simple2.UserAction.execute(UserAction.java:42)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1480)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:524)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)




Line 42 is users.add(uf);
Is there any otherway to add to vector??

Any suggestions ?
thanx
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe your Vector is a null value?
Throw in a println to see if what you are getting from the ServletContext is indeed a value instead of a null.
 
Sahina Celin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx for ur reply.

Yes i tried to print the vector, itz NULL..
How can i get my ActionForm values into Vector?


can u suggest where to modify in my code

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
System.out.println("In execute method of UserAction");
Vector users = (Vector) (request
.getSession()
.getServletContext()
.getAttribute("users"));

UserForm uf = new UserForm();
uf=(UserForm)form;

if ( users == null)
{
System.out.println("Vector is NULL");
}

try{
String sFirstName = uf.getFirstName();
users.add(1,sFirstName);
}catch(Exception e){
System.out.println("Inside exception Part");
e.printStackTrace();
}

request.getSession().getServletContext().setAttribute("users", users);
return (mapping.findForward("registration"));
}


Here im getting nullpointer exception in this line

users.add(1,sFirstName);



can u show me a url, which does this job perfectly.thanx
 
Nate Leech
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you actually creating your users?

From your code, I'm assuming that you have already created a Collection of users somewhere, or you have used a select box with the multi property on for a user to select multiple options via ctrl-click.

If you have use a select box, then you can simply get your users by calling the getter of your users property in your ActionForm.
If not, I can't really say because I don't know what steps have gone on before this one to create your users.

-Nate
 
Sahina Celin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooopz! that was my mistake..
this part of code in LoginAction Servlet got deleted.

if(request.getSession().getServletContext().getAttribute("users") == null){
Vector users = new Vector();
request.getSession().getServletContext().setAttribute("users", users);
}

Now my application is working perfect.
Thanx
 
reply
    Bookmark Topic Watch Topic
  • New Topic