hurry!struts collections error when put checkbox

G

gmjing

i use struts collection,it work fine.but it happen errors when i put
checkbox in every row.the errors like this:

javax.servlet.ServletException: Getter for property ids[0] threw
exception: java.lang.NullPointerException

org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContext-Impl.java:825)


org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextIm-pl.java:758)


org.apache.jsp.queryusera_jsp._jspService(queryusera_jsp.java:110)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:-324)


org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)


org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1-069)


org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProce-ssor.java:455)


org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279-)


org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)


org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


//jsp file
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@page import="com.yourcompany.struts.form.*,java.util.ArrayList"%>


<html>
<head>
<title>JSP for UserForm form </title>
<layout:skin/>


</head>
<body>
<html:form action="/user">
<html:hidden property="oper" value="delUser"/>
<html:submit/><html:cancel/>


<layout:pager maxPageItems="20">
<layout:collection title="aaassss"
name="results"
styleClass="FORM" styleClass2="FORM2"
selectName="ids" selectProperty="name" selectType="checkbox">
<layout:collectionItem title="a" property="name"
sortable="true"/>
<layout:collectionItem title="b" property="dep"
sortable="true"/>
<layout:collectionItem title="c" property="age"
sortable="true"/>
<layout:collectionItem title="d" property="desc"
sortable="true"/>
</layout:collection>
</layout:pager>


</html:form>
</body>
</html>


//struts---Form Class
public class UserForm extends ActionForm {
private String[] ids;
private String age;
private String dep;
private String name;
private String desc;


public String[]getIds() {
return ids;
}


public void setIds(String[]ids) {
this.ids = ids;
}


public String getIds(int index) {
return this.ids[index];
}


public void setIds(int index, String id) {
this.ids[index] = id;
}


public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
return null;
}


public void reset(ActionMapping mapping, HttpServletRequest
request) {


}


public String getAge() {
return age;
}


public void setAge(String age) {
this.age = age;
}


public String getDep() {


return dep;
}


public void setDep(String dep) {
this.dep = dep;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;


}


public String getDesc() {
return desc;
}


public void setDesc(String desc) {
this.desc = desc;
}



}


//struts---Action Class
public class UserAction extends DispatchAction {

public ActionForward qryUser(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserForm userForm = (UserForm) form;


ArrayList l_users = new ArrayList ();


for(int i=0;i<300;i++)
{
UserForm a = new UserForm();
a.setAge("12"+i);
a.setDep("aaa"+i);
a.setDesc("aaaaaaa"+i);
a.setName("aaaa"+i);
l_users.add(a);
}
request.setAttribute("results", l_users);


return mapping.findForward("qrysuc");
}
 
D

Danno

Try:

public class UserForm extends ActionForm {
private String[] ids = new String[10]; //put whatever number
you want
private String age;
private String dep;
private String name;
private String desc;

public String[]getIds() {
return ids;
}

public void setIds(String[]ids) {
this.ids = ids;
}
...


Or :

public class UserForm extends ActionForm {
private String[] ids;
private String age;
private String dep;
private String name;
private String desc;

public UserForm() {
//default no-arg constructor
ids = new String[10]; //put whatever number you want
}
public String[]getIds() {
return ids;
}

public void setIds(String[]ids) {
this.ids = ids;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top