在提交的时候出现下边的错误
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>
&nbsp;
</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">
&nbsp;
<b>系统管理&gt;&gt;用户维护&gt;&gt;添加</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>用户代码:&nbsp;
</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>用户名称:&nbsp;
</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>密码:&nbsp;
</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()">
&nbsp;&nbsp;&nbsp;&nbsp;
<input name="btnBack" class="button1" type="button" id="btnBack"
value="返回" onclick="goBack()" />
</div>
</div>
</form>
</body>
</html>

解决方案 »

  1.   

    <form name="userActionForm" target="_self" id="userActionForm"> 少了一个 action="url"吧
      

  2.   

    AddUserAction 的31行 有空
      

  3.   

       可能是在用实体类的时候用FromBean绑定错误
      

  4.   

    看这里,,uaf是否取不到User啊?其实可以先uaf.getUser(),然后看看是否非空?非空再用BeanUtils.cloneBean(具体名字忘了,有这个clone bean的方法的)过去,不要用copyPropertiesUserActionForm uaf = (UserActionForm)form;
    User user = new User();
    BeanUtils.copyProperties(user, uaf);
    //调用业务逻辑操作
    userService.addUser(user); 
      

  5.   

    AddUserAction.execute(AddUserAction.java:31)  告诉你是这行的错误了,把这行获取的数值打印出来,看看怎么回事。
      

  6.   

    userService.addUser(user); 
    你这里的userService为null。
      

  7.   

    userService.addUser(user);这个为NULL
    SPING 注入的吧?
    看看注入是否正确
      

  8.   

    java.lang.NullPointerException
    at com.tianjian.user.struts.action.AddUserAction.execute(AddUserAction.java:31) 
    AddUserAction.java:31行
      

  9.   

    (AddUserAction.java:31) 看看31行
      

  10.   

    这个是spring这块的配置文件,是这个userService.addUser()有问题,我不知道怎么改, 大家帮我看下啊~  <?xml version="1.0" encoding="UTF-8"?>
    <!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>
      

  11.   

    似乎有点明白了,你的jsp里面,都是这种:
    <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!
      

  12.   

    私聊可以,我只懂struts1.1,hib,spring不懂。q:56525451
      

  13.   

    加个断点 跟一下看看 。那里为空。为什么出现了null。
      

  14.   

    可以肯定是你的UserService没有注入到action中,检查下其他路径注入成功没有?如果都null,那说明你的写法有问题,或者spring的配置文件没有加载成功
      

  15.   

    用了struts,jsp为什么不用struts标签