exception javax.servlet.ServletException
        org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
        org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
        org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
        org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
        org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
        com.Hibernate.SessionFactory.currentSession(SessionFactory.java:56)
        com.sonic.struts.action.LogonAction.execute(LogonAction.java:62)LogonAction代码如下://Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xslt/JavaClass.xslpackage com.sonic.struts.action;import java.util.Iterator;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;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.Hibernate.SessionFactory;
import com.sonic.Userlist;
import com.sonic.struts.form.LogonForm;/** 
* MyEclipse Struts
* Creation date: 12-03-2005

* XDoclet definition:
* @struts.action path="/logon" name="logonForm" input="/form/logon.jsp" scope="request" validate="true"
*/
public class LogonAction extends Action {        // --------------------------------------------------------- Instance Variables        // --------------------------------------------------------- Methods        /** 
         * Method execute
         * @param mapping
         * @param form
         * @param request
         * @param response
         * @return ActionForward
         * @throws HibernateException 
         */
        public ActionForward execute(
                ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response) throws HibernateException {
                LogonForm logonForm = (LogonForm) form;
                // TODO Auto-generated method stub
                
                //获取提交的用户名称和密码
                String userName=request.getParameter("userName");
                String userPwd=request.getParameter("userPwd");
                
                String mUserPwd=null;
                Userlist ul=null;
                //创建连接
                Session session=SessionFactory.currentSession();
                //创建事务
                Transaction tx=session.beginTransaction();
                //创建对话
                Query query=session.createQuery("select u from Userlist as u where username='" +
                                userName + "'");
                try{
                        Iterator it = query.iterate();
                        ul = (Userlist) it.next();
                        mUserPwd=ul.getUserpwd();
                }catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                //事务提交
                tx.commit();
                //关闭连接
                SessionFactory.closeSession();
                if(userPwd.equals(mUserPwd))
                        return (mapping.findForward("gomainMenu"));
                else
                        return (mapping.findForward("rLogon"));
        }}

解决方案 »

  1.   

    我有两点建议:
    1、createQuery("select u from Userlist as u where username='" +userName + "'");中"select的"和select留一个空格。
    2、将 SessionFactory.closeSession();这句话放到finally中去执行。
      

  2.   

    我在myeclipse下写的啊~!没有什么错误提示啊~!应该不是这两个问题吧~帮帮我啊!~大虾们~~~~
      

  3.   

    可能是你的com.Hibernate.SessionFactory初始化不成功,导致
    com.Hibernate.SessionFactory里面的net.sf.hibernate.SessionFactory
    静态对象是空的,确认一下
       一、Hibernate初始化代码是否被正确调用到了
       二、初始化过程中是否有错
      

  4.   

    具体怎么样你最好贴出你的com.Hibernte.SessionFactory代码
      

  5.   

    好的~!
    com.Hibernte.SessionFactory代码如下:package com.Hibernate;import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.cfg.Configuration;/**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html}.
     */
    public class SessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * NOTICE: Location should be on the classpath as Hibernate uses
         * #resourceAsStream style lookup for its configuration file. That
         * is place the config file in a Java package - the default location
         * is the default Java package.<br><br>
         * Examples: <br>
         * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
         * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> 
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";    /** Holds a single instance of Session */
        private static final ThreadLocal threadLocal = new ThreadLocal();    /** The single instance of hibernate configuration */
        private static final Configuration cfg = new Configuration();    /** The single instance of hibernate SessionFactory */
        private static net.sf.hibernate.SessionFactory sessionFactory;    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session currentSession() throws HibernateException {
            Session session = (Session) threadLocal.get();        if (session == null) {
                if (sessionFactory == null) {
                    try {
                        cfg.configure(CONFIG_FILE_LOCATION);
                        sessionFactory = cfg.buildSessionFactory();
                    }
                    catch (Exception e) {
                        System.err.println("%%%% Error Creating SessionFactory %%%%");
                        e.printStackTrace();
                    }
                }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }        return session;
        }    /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
                session.close();
            }
        }    /**
         * Default constructor.
         */
        private SessionFactory() {
        }}
    帮帮我啊~!侠士们~!虽然没有什么分数~!但帮人一次胜过七条人命啊~~~~谢谢大家了~