在提交的时候出现下边的错误
2009-12-09 17:17:39,734 [org.apache.struts.action.RequestProcessor]-[WARN] Unhandled Exception thrown: class java.lang.NullPointerException
2009-12-09 17:17:39,750 [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Test1].[action]]-[ERROR] Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at com.tianjian.user.struts.action.AddUserAction.execute(AddUserAction.java:31)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.tianjian.core.hibernate.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:127)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)下边这个是AddUserAction
package com.tianjian.user.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.tianjian.user.bean.User;
import com.tianjian.user.business.UserService;
import com.tianjian.user.struts.form.UserActionForm;public class AddUserAction extends Action {
private UserService userService;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//获取从页面表单中提交过来的值
UserActionForm uaf = (UserActionForm)form;
User user = new User();
BeanUtils.copyProperties(user, uaf);
//调用业务逻辑操作
userService.addUser(user);
return mapping.findForward("success");
}
public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}
}下边这个是user_input.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>添加用户</title>
<link rel="stylesheet" href="../style/drp.css">
<script src="../script/client_validate.js"></script>
<script type="text/javascript"> function addUser() {
with (document.getElementById("userActionForm")) {
method = "post";
action = "add.do";
submit();
}
} function goBack() {
window.self.location ="list.do"
}
function init() {
document.userForm.userId.focus();
}
</script>
</head> <body class="body1">
<form name="userActionForm" target="_self" id="userActionForm">
<div align="center">
<table width="95%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
</td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0"
height="25">
<tr>
<td width="522" class="p1" height="25" nowrap>
<img src="../images/_arrow_03.gif" width="14" height="14">
<b>系统管理>>用户维护>>添加</b>
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<table width="95%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="22%" height="29">
<div align="right">
<font color="#FF0000">*</font>用户代码:
</div>
</td>
<td width="78%">
<input name="userId" type="text" class="text1" id="userId"
size="10" maxlength="10">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>用户名称:
</div>
</td>
<td>
<input name="userName" type="text" class="text1" id="userName"
size="20" maxlength="20">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>密码:
</div>
</td>
<td>
<label>
<input name="password" type="password" class="text1"
id="password" size="20" maxlength="20">
</label>
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<div align="center">
<input name="btnAdd" class="button1" type="button" id="btnAdd"
value="添加" onClick="addUser()">
<input name="btnBack" class="button1" type="button" id="btnBack"
value="返回" onclick="goBack()" />
</div>
</div>
</form>
</body>
</html>
2009-12-09 17:17:39,734 [org.apache.struts.action.RequestProcessor]-[WARN] Unhandled Exception thrown: class java.lang.NullPointerException
2009-12-09 17:17:39,750 [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Test1].[action]]-[ERROR] Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at com.tianjian.user.struts.action.AddUserAction.execute(AddUserAction.java:31)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.tianjian.core.hibernate.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:127)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)下边这个是AddUserAction
package com.tianjian.user.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.tianjian.user.bean.User;
import com.tianjian.user.business.UserService;
import com.tianjian.user.struts.form.UserActionForm;public class AddUserAction extends Action {
private UserService userService;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//获取从页面表单中提交过来的值
UserActionForm uaf = (UserActionForm)form;
User user = new User();
BeanUtils.copyProperties(user, uaf);
//调用业务逻辑操作
userService.addUser(user);
return mapping.findForward("success");
}
public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}
}下边这个是user_input.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>添加用户</title>
<link rel="stylesheet" href="../style/drp.css">
<script src="../script/client_validate.js"></script>
<script type="text/javascript"> function addUser() {
with (document.getElementById("userActionForm")) {
method = "post";
action = "add.do";
submit();
}
} function goBack() {
window.self.location ="list.do"
}
function init() {
document.userForm.userId.focus();
}
</script>
</head> <body class="body1">
<form name="userActionForm" target="_self" id="userActionForm">
<div align="center">
<table width="95%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
</td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0"
height="25">
<tr>
<td width="522" class="p1" height="25" nowrap>
<img src="../images/_arrow_03.gif" width="14" height="14">
<b>系统管理>>用户维护>>添加</b>
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<table width="95%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="22%" height="29">
<div align="right">
<font color="#FF0000">*</font>用户代码:
</div>
</td>
<td width="78%">
<input name="userId" type="text" class="text1" id="userId"
size="10" maxlength="10">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>用户名称:
</div>
</td>
<td>
<input name="userName" type="text" class="text1" id="userName"
size="20" maxlength="20">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>密码:
</div>
</td>
<td>
<label>
<input name="password" type="password" class="text1"
id="password" size="20" maxlength="20">
</label>
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<div align="center">
<input name="btnAdd" class="button1" type="button" id="btnAdd"
value="添加" onClick="addUser()">
<input name="btnBack" class="button1" type="button" id="btnBack"
value="返回" onclick="goBack()" />
</div>
</div>
</form>
</body>
</html>
User user = new User();
BeanUtils.copyProperties(user, uaf);
//调用业务逻辑操作
userService.addUser(user);
你这里的userService为null。
SPING 注入的吧?
看看注入是否正确
at com.tianjian.user.struts.action.AddUserAction.execute(AddUserAction.java:31)
AddUserAction.java:31行
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean name="/user/add" class="com.tianjian.user.struts.action.AddUserAction">
<property name="userService">
<ref local="UserService"/>
</property>
</bean>
<bean name="/user/del" class="com.tianjian.user.struts.action.DelUserAction">
<property name="userService">
<ref local="UserService"/>
</property>
</bean>
<bean name="/user/find" class="com.tianjian.user.struts.action.FindUserAction">
<property name="userService">
<ref local="UserService"/>
</property>
</bean> <bean name="/user/list" class="com.tianjian.user.struts.action.ListUserAction">
<property name="userService">
<ref local="UserService"/>
</property>
</bean>
<bean name="/user/modify" class="com.tianjian.user.struts.action.ModifyUserAction">
<property name="userService">
<ref local="UserService"/>
</property>
</bean>
<bean id="UserService" parent="BaseTxService">
<property name="target">
<bean class="com.tianjian.user.business.service.UserServiceImpl">
<property name="userDAO">
<ref bean="UserDAO" />
</property>
</bean>
</property>
</bean>
<bean id="UserDAO" class="com.tianjian.user.DAO.hibernate.UserDAO" singleton="false">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
<input name="userId" type="text" class="text1" id="userId" size="20" maxlength="20">
<input name="userName" type="text" class="text1" id="userName" size="20" maxlength="20">
<input name="password" type="password" class="text1" id="password" size="20" maxlength="20">
而在你的Form里面,似乎User是一个对象,并没有把User的属性直接放在FormBean里面。
所以,界面上的这些,包括form最好都改为struts标签标记,如html:form(2.0没有区分了都是一个相同的prefix打头)。既然你用了struts,干吗不用taglib?
如果一定不用taglib,也要在name和id前面加上formbean里面的变量名。<input name="user.userId" type="text" class="text1" id="user.userId"
就是说,这里我加的user对应 于你UserActionForm 里面对应的那个User对象的变量名称,而userId对应于User类里面的userId变量。其他的userName、password也要做相应的改动。good luck!