因为你在static块中初始化时它有可能抛出异常,,如果抛出异常了你的这个常量就初始化不了了..所以编译器告警了.
要:
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex)
{
log.error("Initial SessionFactory creation failed.",ex);
sessionFactory =null;
} 就不会报了..不过这样你的程序中访问时还是会出错的.
要:
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex)
{
log.error("Initial SessionFactory creation failed.",ex);
sessionFactory =null;
} 就不会报了..不过这样你的程序中访问时还是会出错的.
解决方案 »
- tomcat 安装问题 failed to install tomcat6 service ,check your setting and permissions
- 用xfire开发简单的webservice 时遇到的问题
- oracle連接問題
- web项目部署到tomcat6中出现HTTP Status 500错误
- 需要一个JAVA开源的BLOG!!!!!!!!!!!11
- 高分!高手请!关于网络搜索的一个问题!
- 关于xml的:我查了很多网上资源,都是互相抄袭,都不对(在线等待)
- 我的http://localhost:8080/jmx-console/出错。。。
- 大侠请进,部署成功,但是调用出错,求解
- SpringMvc报错求帮助啊,大神们
- 大家来看看为什么我的ActionForm不能得到FormFile,谢谢
- 在Spring 中使用基于Quartz的定时任务 出现了错误,请做过这方面的朋友帮帮忙
抛出了java.lang.NullPointerException
package persistence;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public class HibernateUtil
{
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex)
{
log.error("Initial SessionFactory creation failed.",ex);
//sessionFactory=null;
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException
{
Session s=sessionFactory.openSession();//这里抛出了NullPointerException
return s;
}
public static void closeSession() throws HibernateException
{
Session s=(Session)session.get();
session.set(null);
if(s!=null)
s.close();
}
}
这又是为什么呢?初学者看书的,手写上去的,可是遇到这样的问题!
常量是只会被初始化一次的,其中初始化又可以用两种方法:
1,直接声明时初始化,如
final String STR = "STR";
2,如果需要在复杂的计算后再给这个常量值,就在static中写你的代码,如:
final String STR;
static{
//do something..
STR = "STR";//而且中这段代码中必须保证这个常量被赋值一次,当然这里你甚至可以调用静态方法.
}而你上面的代码中,
private static SessionFactory sessionFactory; //这么定义的话必须要有一个static代码块来对它进行初始化
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory(); //这个初始化有可能失败..那失败了后这个常量实际上没有值,没有值和null是两回事要搞清楚哦?..它必须要接受一个等号的赋值操作才算.
}
catch(Throwable ex)
{
log.error("Initial SessionFactory creation failed.",ex);
//所以出错了,,你必须对它初始化.Eclipse的编译器动态告诉你了..只是你不明白..
sessionFactory=null; //至于这里出错了..至少你如果上面用final定义的话,这里这句一定要的.那出错是了就是你的程序有问题.
//你就需要在这里写代码来处理了..不然你的程序在运行时就会报错..
}
}不知道这样说,你现在还是不能明白?
Student类:
package model;
public class Student
{
public String id;
public String name;
public String cardId;
public int age;
}
下面是hbm.xml的内容:
<hibernate-mapping>
<class name="model.Student" table="student_table"><!--把类和数表关联起来!-->
<id name="id" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="cardId" type="String"/>
<property name="name" type="String"/>
<property name="age" type="int"/>
</class>
</hibernate-mapping>
我想知道一下cfg.xml什么写,可能我在映射数据库的时候错误了,找不到数据库。
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex)
{
log.error("Initial SessionFactory creation failed.",ex);
sessionFactory =null;//终态字段sessionFactory可能已经赋值了
}
但是当我加上sessionFactory=null这句的时候,出现了终态字段sessionFactory可能已经赋值了。
这又是什么回事呢?还有当我去掉final终态字的时候就在这个类中没有什么错了,但是当我编译我的工程时又出现了
NullPointException