当前位置:编程学习 > JAVA >>

java.lang.NullPointerException的问题

package com.zxcs.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.zxcs.interceptor.IsLoginedInterceptor; import com.opensymphony.xwork2.ActionSupport; import com.zxcs.bean.*; import com.zxcs.service.UserService; public class LoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private User user; private UserService service; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getService() { return service; } public void setService(UserService service) { this.service = service; } public void validate() { if(this.getUser().getUsername()==null||" ".equals(this.getUser().getUsername())){ this.addFieldError(this.getUser().getUsername(), "用户名不能为空!"); } if(this.getUser().getUserpassword()==null||" ".equals(this.getUser().getUserpassword())){ this.addFieldError(this.getUser().getUsername(),"不能为空!"); } } @Override public String execute() throws Exception { User user1 = null; HttpServletRequest req=ServletActionContext.getRequest(); user1 = this.service.findLogin(this.getUser().getUsername(),this.getUser().getUserpassword()); if(user1!=null){ req.getSession(true).setAttribute(IsLoginedInterceptor.USER_SESSION_KEY, user1);//把用户信息保存到Session里,方便获取 return SUCCESS; }else{ System.out.println("用户名和密码错误!"); this.addFieldError(this.getUser().getUsername(), "户名和密码错误!"); //ActionError("用户名和密码错误!"); return INPUT; } } } java.lang.NullPointerException com.zxcs.action.LoginAction.validate(LoginAction.java:38) com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:249) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:261) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
追问:有可能是getuser()这个为空,那怎么改呢
答案:if(this.getUser().getUsername()==null||" ".equals(this.getUser().getUsername())){
this.addFieldError(this.getUser().getUsername(), "用户名不能为空!");
//你前面都判断出this.getUser().getUsername()==null,下面还执行this.getUser().getUsername()
//能不报空么
其他:这个错误意思是  有些对象引用是空引用    没有指向一个具体的对象   自己检查一下 user1 = this.service.findLogin(this.getUser().getUsername(),this.getUser().getUserpassword());
	
	     if(user1!=null){ LoginAction.java:38
第38行错误,你检查下,是不是没有获取到request对象。 

上一个:JAVA多选:下面不是 JAVA的复合数据类型的是
下一个:谁能给我提供一下java的QQ群?谢谢啦

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,