日期:2014-05-19  浏览次数:20672 次

大侠们帮忙看看
html:
<form action="reg" method="post">
  <dl class="reg">
  <dd>昵称:<input type="text" name="name" class="username"/></dd>
  <dd>密码:<input type="password" name="password" class="username"/></dd>
  <dd>确认密码:<input type="password" name="password1" class="username"/></dd>
  <dd>邮箱:<input type="text" name="email" class="username"/></dd>
  <dd>性别:&nbsp;&nbsp;<input type="radio" name="sex" value="1"/>&nbsp;&nbsp;&nbsp;男&nbsp;&nbsp;&nbsp; <input type="radio" name="sex" value="0"/> 女&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</dd>
  <dd><input type="submit" name="send" value="注册"/></dd>
  </dl>
  </form>
struts:
 <constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
   
  <action name="reg" class="edu.gsau.action.UserAction" method="add">
  <result>
  /reg.jsp
  </result>
  </action>
  </package>
User.java:
package edu.gsau.model;

public class User {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
private String name ;
private String password;
private String email;
private String sex;

}
UserAction.java
package edu.gsau.action;

import com.opensymphony.xwork2.ActionSupport;

import edu.gsau.model.User;

public class UserAction extends ActionSupport{
User user ;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String add(){
System.out.println(user.getName());
return SUCCESS;
}

}
===========================================================
error:
Stacktraces
java.lang.NullPointerException 
  edu.gsau.action.UserAction.add(UserAction.java:16)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

.......

------解决方案--------------------
你这样写当然不行,空指针错误,你的user是空值,空指针错误
1.要不你去实现ModelDriven接口。
2.要不在action类里写好jsp传来参数的setter方法。
3.要不在JSP页面,用user.username的方式传值