刚学hibernate,第一个实例是视频里面的,我都按着操作来的,但是我就是找不到问题出在哪里。异常:java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:330)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:296)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:300)
at StudentTest.main(StudentTest.java:17)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:608)
... 4 morehibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/spring</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>
        <!-- JDBC connection pool (use the built-in) -->
        <!-- <property name="connection.pool_size">1</property> -->
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySqlDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
       <!--  <property name="hbm2ddl.auto">update</property> -->
        <mapping resource="com/model/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>student.javapackage com.model;public class Student {
    private Integer id;
    private String name;
    private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

}Student.hbm.xml<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.model">
       <class name="Student" table="student">
           <id name="id"></id>
           <property name="name" type="String"></property>
           <property name="age" type="int"></property>
       </class>
</hibernate-mapping>
测试类StudentTest.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import com.model.Student;
public class StudentTest {
    public static void main(String[] args) {
Student student=new Student();
student.setId(1);
student.setName("mlwise");
student.setAge(22);
try {
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
s.save(student);
s.getTransaction().commit();
s.close();
sf.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("这里出了问题");
}
catch (Throwable e) {
e.printStackTrace();
}



}
}请各位大虾帮忙,谢谢!!

解决方案 »

  1.   

     <property name="age" type="int"></property>
    把int改成integer看看
      

  2.   

    Configuration cfg=new Configuration();
    SessionFactory sf=cfg.configure().buildSessionFactory();
    Session s=sf.openSession();
    s.beginTransaction();
    s.save(student);
    s.getTransaction().commit();
    s.close();
    sf.close();这个单步调试试试,从try里的第一行代码打断点,开始,看执行到哪行,跳异常了
    看到底执行到哪行出问题,就可以定位了
      

  3.   

    StudentTest.main(StudentTest.java:17)
    这行你检查下问题
      

  4.   

    at StudentTest.main(StudentTest.java:17)StudentTest.java第17行报空了撒
    Caused by: java.lang.NullPointerException
      

  5.   

    你没有看到 是 init 初始化问题么???ublic class Student {
      private Integer id;
      private String name;
      private Integer age; 这个地方以后 不要用 java 的包装类型。。这样会导致你的xml文件中。的 类型 不对。。用int 。。你再试试。。我估计是这个地方的问题
      

  6.   


    那行应该没错,我在网上看到好多例子都是这么写的,我配置hibernate的时候用的是默认文件名。
      

  7.   

    没指定hibernate配置文件。SessionFactory sf = new Configuration()
        .configure("catdb.cfg.xml")
        .buildSessionFactory();
      

  8.   

    我已经全部改成了Integer的类型,但是还是没用。
      

  9.   


    指定了hibernate的配置文件也是一样的异常,网上说hibernate.cfg.xml是默认的,只要是默认就无需指定,所以我使用了.configure()方法,而没有使用.configure("hibernate.cfg.xml")
      

  10.   

    配置文件你放在哪里了?要是默认的放在src下
      

  11.   


    我是放在src下,我还尝试过放在WEB-INF目录下,基本可以放的地方都试过了。就是不行。我用的是3.6的hibernate 和slf4 1.6.1
      

  12.   

    谢谢大家这么热心提出问题,目前我自己解决了这个问题,无意中想到的。原来是我在创建user library的时候,勾选了<input type="checkbox">system library (added to the boot class path)选项,我创建项目的时候添加hibernate capabilities的时候,用build path->add library—》自己的user library。主要就是问题就是出在创建user library的时候勾选了system library选项。去掉勾选后,既然好了。