大家帮帮忙啦。struts2+hibernate
hibernate自动生成的DAO跟实体类,在做登录时用struts2做的,可是调用到findByExample()时就报了 java.lang.NullPointerException。。。帮帮忙,小女子万分感激。 --------------------编程问答-------------------- 代码贴出来 亲 --------------------编程问答-------------------- 第一反应检查getter setter有没有 --------------------编程问答-------------------- 1.检查getter setter方法
2.检查注解是否有写。
3.以上都没问题的话,把导致错误的类的代码贴出来看看。 --------------------编程问答--------------------
login.jsp
<form action="/PinCan/login" method="post">
账户名:<input type="text" name="username" id="username" />
密 码:<input type="password" name="userpass" id="userpass" />
<input type="submit" value="登录" />
<a href="register.jsp">免费注册</a>
</form>
struts.xml
<package name="login" extends="struts-default">
<action name="login" class="com.pincan.www.action.LoginAction">
<result name="success" type="dispatcher">/index.jsp</result>
<result name="error" type="dispatcher">/error.jsp</result>
</action>
</package>
LoginAction.java
package com.pincan.www.action;
import org.apache.struts2.ServletActionContext;
import com.pincan.www.po.*;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
/**
* @return
*/
String username;
String userpass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String execute() {
UserInfos userinfo=new UserInfos();
userinfo.setUserpass(userpass);
userinfo.setUsername(username);
UserInfosDAO userinfosDAO=new UserInfosDAO();
if(userinfosDAO.findByExample(userinfo).isEmpty())
return "error";
else {
ServletActionContext.getRequest().getSession().setAttribute("userinfo", username);
return "success";
}
}
}
UserInfosDAO.java 的findByExample()方法
public List findByExample(UserInfos instance) {
log.debug("finding UserInfos instance by example");
try {
List results = getSession().createCriteria(
"com.pincan.www.po.UserInfos")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
--------------------编程问答-------------------- 它报了findByExample()中List results = getSession().createCriteria("com.pincan.www.po.UserInfos")
有错。可是我不知道怎么改? --------------------编程问答-------------------- com.pincan.www.po.UserInfos这个类贴出来看看呢。 --------------------编程问答--------------------
UserInfos.java
package com.pincan.www.po;
public class UserInfos implements java.io.Serializable {
// Fields
private Integer userid;
private String username;
private String userpass;
private String phone;
// Constructors
/** default constructor */
public UserInfos() {
}
/** full constructor */
public UserInfos(String username, String userpass, String phone) {
this.username = username;
this.userpass = userpass;
this.phone=phone;
}
// Property accessors
public Integer getUserid() {
return this.userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return this.userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
} --------------------编程问答-------------------- getSession().createCriteria("com.pincan.www.po.UserInfos")
这里并不是要你传个路径 是要传class 所以改成
--> getSession().createCriteria(com.pincan.www.po.UserInfos.class) 这样改试试!
--------------------编程问答--------------------
这是hibernate自动生成的哦 --------------------编程问答--------------------
你改下试下行吗? 我也是网上看的! 不行就把你配置文件都贴出来还有你报的错是那一行异常信息贴出来!
不知道你说的那行是
List results = getSession().createCriteria( 1
"com.pincan.www.po.UserInfos") 2
.add(Example.create(instance)).list(); 3
这个1行还是他们3行就是一行! --------------------编程问答--------------------
list为空啊。
java.lang.NullPointerException
com.pincan.www.po.UserInfosDAO.findByExample(UserInfosDAO.java:67)
com.pincan.www.action.LoginAction.execute(LoginAction.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:
--------------------编程问答-------------------- list为空?你是这么理解? list会报异常?
你调试一下看看 getSession()是什么 是否有值 是否为空
补充:Java , Java EE