java.lang.NullPointerException

cleveben 2009-03-24 09:57:30
LoginAction调用DAO时出现空指针异常,代码如下:

package com.hotel.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.hotel.hibernate.dao.OperatorDAO;
import com.hotel.struts.form.LoginForm;
import com.hotel.struts.util.Constants;


public class LoginAction extends DispatchAction {

protected OperatorDAO operatorDAO ;

Logger log = Logger.getLogger(this.getClass());

public OperatorDAO getOperatorDAO() {
return operatorDAO;
}

public void setOperatorDAO(OperatorDAO operatorDAO) {
this.operatorDAO = operatorDAO;
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionMessages messages = new ActionMessages();
ActionForward forward = new ActionForward();
LoginForm loginForm = (LoginForm) form;

// try {
// get parameters
String oid = loginForm.getOid();

// invalidate the original session if exists
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}

// create a new session for the user
session = request.getSession(true);

// login
boolean isValid = valid(request, loginForm);
if (isValid) {

session.setAttribute(Constants.OPERATOR_KEY, oid);

log.info("Operator " + oid + " login.");
} else {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"login.message.failed"));
}

// } catch (Exception e) {
// messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
// "register.message.failed"));
// }

// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!messages.isEmpty()) {
saveErrors(request, messages);
request.setAttribute("loginFormBean", loginForm);
forward = mapping.findForward(Constants.FAILURE_KEY);
} else {
forward = mapping.findForward(Constants.SUCCESS_KEY);
}

// Finish with
return (forward);
}

private boolean valid(HttpServletRequest request, LoginForm loginForm) {
if (getOperatorDAO().isValid(loginForm.getOid(),
loginForm.getOpwd())) {
return true;
} else {
return false;
}
}

}
...全文
87 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Study_Work_2009 2009-03-25
  • 打赏
  • 举报
回复
如果spring配置没错的话,不需要new OperatorDAO()啊
我估计就是OperatorDAO没有注入
cleveben 2009-03-24
  • 打赏
  • 举报
回复
改成 protected OperatorDAO operatorDAO = new OperatorDAO() ; 还是不行
spring的配置应该没错

OperatorDAO的相关代码:

public boolean isValid(final String oid, final String opwd) {
List list = (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
List result = session.createCriteria(Operator.class).add(
Restrictions.eq("oid", oid)).add(
Restrictions.eq("opwd", opwd)).list();
return result;
}
});
if (list.size() > 0) {
return true;
} else {
return false;
}
}
blueram 2009-03-24
  • 打赏
  • 举报
回复
你是用spring对
operatorDAO 进行设值吗
看看配置有误没有
zhj92lxs 2009-03-24
  • 打赏
  • 举报
回复
我没有发现你new了operatorDAO

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧