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

java.lang.NullPointerException

出现java.lang.NullPointerException
at com.mwq.hibernate.BaseDao.queryList(BaseDao.java:30)
at com.mwq.hibernate.Dao.queryManagerOfNatural(Dao.java:141)


提示错误代码部分:
//basedao
protected List queryList(String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);//提示这行错误
List list = query.list();
return list;
}

//DAO
public List queryManagerOfNatural() {
return this.queryList("from TbManager where state='正常'");//cuowu
}
session nullpointerexception --------------------编程问答-------------------- HibernateSessionFactory这个应该是空吧 --------------------编程问答-------------------- debug一下 看看变量session 的值 应该为空,估计是数据库配置错了 没有创建好dataSource --------------------编程问答-------------------- HibernateSessionFactory没有生成 --------------------编程问答-------------------- 楼主是怎么获取HibernateSessionFactory的? --------------------编程问答-------------------- session值为空 --------------------编程问答--------------------
引用 3 楼 splendid_java 的回复:
HibernateSessionFactory没有生成


引用 2 楼 benlaten 的回复:
debug一下 看看变量session 的值 应该为空,估计是数据库配置错了 没有创建好dataSource
数据库我是使用imp导入的。debug了之后, --------------------编程问答-------------------- 看你的配置文件吧,应该是那的问题,获取不到session --------------------编程问答--------------------
引用 6 楼 m625879987 的回复:
Quote: 引用 3 楼 splendid_java 的回复:

HibernateSessionFactory没有生成


引用 2 楼 benlaten 的回复:
debug一下 看看变量session 的值 应该为空,估计是数据库配置错了 没有创建好dataSource
数据库我是使用imp导入的。debug了之后,
watch下变量,或者System.out.println(session); --------------------编程问答-------------------- 你是不是用了Struts ,应该是Hibernate的包和Struts冲突了,各种框架的包不要全部加进去,用到什么加什么 --------------------编程问答-------------------- HibernateSessionFactory.getSession();
代码贴出来看看 --------------------编程问答--------------------
引用 10 楼 ch656409110 的回复:
HibernateSessionFactory.getSession();
代码贴出来看看

public class HibernateSessionFactory {

private static SessionFactory sessionFactory;

private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
System.out.println("------在加载Hibernate配置文件时抛出异常,内容如下:");
e.printStackTrace();
}
}

public static Session getSession() {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}/*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 closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null || session.isOpen()) {
session.close();
}
}

} --------------------编程问答--------------------
引用 10 楼 ch656409110 的回复:
HibernateSessionFactory.getSession();
代码贴出来看看
package com.mwq.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

private static SessionFactory sessionFactory;

private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
System.out.println("------在加载Hibernate配置文件时抛出异常,内容如下:");
e.printStackTrace();
}
}

public static Session getSession() {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null || session.isOpen()) {
session.close();
}
}

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