在用struts进行bean拷贝的时候,出现空指针异常,我用的是MyEclipse5.1 struts1.2和resin2.1.7服务器 下面我吧我的action和copybean代码发过来,请各位前辈给指点到低是哪里的问题,先谢谢啦 action代码 public class UserAction extends Action {
/*
* Generated Methods
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
UserActionForm uf = (UserActionForm)form;
UserDto ut = new UserDto();
CopyBean cb = new CopyBean();
try {
cb.copyAttribute(uf, ut);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UserServer us = new UserServerImpl();
us.addUser(ut);
String str = "suce";
return mapping.findForward(str);
}
} bean拷贝代码import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;public class CopyBean {
public static Object copyAttribute(Object source,Object dest) throws
InvocationTargetException, IllegalArgumentException,
IllegalAccessException { String sourceMethodName = null;
String sourceMethodFix = null;
String destMethodName = null;
String destMethodFix = null;
Method[] sourceMethod=source.getClass().getMethods();
Method[] destMethod=dest.getClass().getMethods();
for(int i=0;i<sourceMethod.length;i++){
sourceMethodName=sourceMethod[i].getName();
sourceMethodFix=sourceMethodName.substring(3);
if(sourceMethodName.startsWith("get")){
for(int n=0;n<destMethod.length;n++){
destMethodName=destMethod[n].getName();
destMethodFix=destMethodName.substring(3);
if(destMethodName.startsWith("set")){
if(sourceMethodFix.equals(destMethodFix)){
Object[] sourceArray=new Object[0];
Object[] destArray=new Object[1];
//激活source的相应的get的方法,sourceArray数组存放调用该方法的参数,此例中没有参数,该数组的长度为0
destArray[0]=sourceMethod[i].invoke(source,sourceArray);
//激活dest的相应的set的方法,destArray数组存放调用该方法的参数
destMethod[n].invoke(dest,destArray);
continue;
}
}
}
}
}
return dest;
}
} 出的异常是这样的500 Servlet Exception
java.lang.NullPointerException
at util.CopyBean.copyAttribute(CopyBean.java:15)
at action.UserAction.execute(UserAction.java:40)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:165)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:253)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:171)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:534)
/*
* Generated Methods
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
UserActionForm uf = (UserActionForm)form;
UserDto ut = new UserDto();
CopyBean cb = new CopyBean();
try {
cb.copyAttribute(uf, ut);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UserServer us = new UserServerImpl();
us.addUser(ut);
String str = "suce";
return mapping.findForward(str);
}
} bean拷贝代码import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;public class CopyBean {
public static Object copyAttribute(Object source,Object dest) throws
InvocationTargetException, IllegalArgumentException,
IllegalAccessException { String sourceMethodName = null;
String sourceMethodFix = null;
String destMethodName = null;
String destMethodFix = null;
Method[] sourceMethod=source.getClass().getMethods();
Method[] destMethod=dest.getClass().getMethods();
for(int i=0;i<sourceMethod.length;i++){
sourceMethodName=sourceMethod[i].getName();
sourceMethodFix=sourceMethodName.substring(3);
if(sourceMethodName.startsWith("get")){
for(int n=0;n<destMethod.length;n++){
destMethodName=destMethod[n].getName();
destMethodFix=destMethodName.substring(3);
if(destMethodName.startsWith("set")){
if(sourceMethodFix.equals(destMethodFix)){
Object[] sourceArray=new Object[0];
Object[] destArray=new Object[1];
//激活source的相应的get的方法,sourceArray数组存放调用该方法的参数,此例中没有参数,该数组的长度为0
destArray[0]=sourceMethod[i].invoke(source,sourceArray);
//激活dest的相应的set的方法,destArray数组存放调用该方法的参数
destMethod[n].invoke(dest,destArray);
continue;
}
}
}
}
}
return dest;
}
} 出的异常是这样的500 Servlet Exception
java.lang.NullPointerException
at util.CopyBean.copyAttribute(CopyBean.java:15)
at action.UserAction.execute(UserAction.java:40)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:165)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:253)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:171)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:534)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货