aspose file tools*
The moose likes Struts and the fly likes Adding to vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Adding to vector" Watch "Adding to vector" New topic
Author

Adding to vector

Sahina Celin
Greenhorn

Joined: Aug 25, 2004
Posts: 23
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
Nate Leech
Ranch Hand

Joined: Feb 07, 2005
Posts: 43
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

Joined: Aug 25, 2004
Posts: 23
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

Joined: Feb 07, 2005
Posts: 43
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

Joined: Aug 25, 2004
Posts: 23
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding to vector