public Member memLogin(String loginName, String loginPwd) throws Exception {
Session session = MySessionFactory.getSession();
Transaction tx = null;
Member mem = null;
try{
String hql = "select a from Member as a where a.loginName=:loginName and a.loginPwd=:loginPwd";
Query query = session.createQuery(hql);
query.setString("loginName", loginName);
query.setString("loginPwd", loginPwd);
query.setMaxResults(1);
tx = session.beginTransaction();
mem = (Member)query.uniqueResult();
mem.setLoginTimes(Integer.valueOf(mem.getLoginTimes().intValue()+1));
mem.setLastDate(new Date());
session.update(mem);
tx.commit();
}catch(Exception ex){
if(tx!=null)tx.rollback();
ex.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
return mem;
}Query query = session.createQuery(hql);抛出异常,“java.lang.NullPointerException”
高手指教

解决方案 »

  1.   

    没有获得session?MySessionFactory怎么写的
      

  2.   

    session is null?
    不知道你是怎么获得Session的
    不用Spring的情况下我一般是这样拿的:
    Configuration cfg = new Configuration().configure();
    SessionFactory factory = cfg.buildSessionFactory(); 
    Session session = factory.openSession();或
    Session session = factory.getCurrentSession();
      

  3.   

    我的sessionfactory这样写的,以前也这样写的,都可以用。不知道为什么这里就出错。有高手知道吗?public class MySessionFactory {
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private MySessionFactory() {
        }
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        } public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    }     public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
                session.close();
            }
        }
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    } public static void setConfigFile(String configFile) {
    MySessionFactory.configFile = configFile;
    sessionFactory = null;
    } public static Configuration getConfiguration() {
    return configuration;
    }}
      

  4.   

    查看下你hibernate.cfg.xml中的配置,是否url有修改过了
      

  5.   

    URL应该没有错的,之前连接过另一个名字的数据库事可以的。烦恼啊烦恼
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
     <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=yt
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>
    <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
    </property>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property>
         
         <!-- 配置C3P0连接池属性 -->
    <property name="hibernate.connection.provider_class">
    org.hibernate.connection.C3P0ConnectionProvider
    </property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.timeout">50000</property>
    <property name="hibernate.c3p0.max_statements">100</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.c3p0.validate">false</property>
      - 
    - <!--   注册ORM映射文件 
      
      --> 
      <mapping resource="com/ORM/Memberlevel.hbm.xml" /> 
      <mapping resource="com/ORM/Merchandise.hbm.xml" /> 
      <mapping resource="com/ORM/Admin.hbm.xml" /> 
      <mapping resource="com/ORM/Orders.hbm.xml" /> 
      <mapping resource="com/ORM/Cartselectedmer.hbm.xml" /> 
      <mapping resource="com/ORM/Cart.hbm.xml" /> 
      <mapping resource="com/ORM/Leaveword.hbm.xml" /> 
      <mapping resource="com/ORM/Category.hbm.xml" /> 
      <mapping resource="com/ORM/Member.hbm.xml" /> 
      <mapping resource="com/ORM/QuotedPricer.hbm.xml" />   </session-factory>
    </hibernate-configuration>
      

  6.   

    用debug跟踪一下看看到底是执行过程是怎样的
    检查看看hibernate.cfg.xml是不是放对地方了
    hibernate.cfg.xml直接放在src下就可以直接configuration.configure(); 
      

  7.   

    问题解决了。。有点无奈
    发现<!--  注册ORM映射文件 
      
      --> 这句话前面的“-”了吗。
    我删了后居然好了。谢谢大家了