自己写的小工程,JUnit测试总是报下面的异常:
java.lang.NullPointerException
at com.bjsxt.hibernate.model.HibernateIDTest.afterClass(HibernateIDTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

解决方案 »

  1.   

    <?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>
       
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/test</property>
            <property name="connection.username">root</property>
            <property name="connection.password">123456</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            
           <!--
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
           <property name="dialect">org.hibernate.dialect.OracleDialect</property>
           -->
            
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>        <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>        <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>
    <mapping class="com.bjsxt.hibernate.model.Student"/>
        </session-factory></hibernate-configuration>
      

  2.   

    <?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>
    <class name="com.bjsxt.hibernate.model.Student">
    <id name="id">
    <generator class="uuid"></generator>
    </id>

    <!--
    <composite-id name="pk" class="com.bjsxt.hibernate.StudentPK">
    <key-property name="id"></key-property>
    <key-property name="name"></key-property>
    </composite-id>
    -->

    <property name="age" />
    <property name="sex" />
    <property name="good" type="yes_no"></property>
        </class>
    </hibernate-mapping>
      

  3.   

    测试类的代码:
    package com.bjsxt.hibernate.model;import java.util.Date;import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.hibernate.service.ServiceRegistryBuilder;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;public class HibernateIDTest
    {
    private static SessionFactory sessionFactory; private static SessionFactory buildSessionFactory()
    throws HibernateException
    {
    Configuration cfg = new Configuration();
    cfg.configure();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
    SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
    return sf;
    } public static SessionFactory getSessionFactory()
    {
    return sessionFactory;
    }
    @BeforeClass
    public static void beforeClass()
    {
    // sessionFactory = new AnnotationConfiguration().configure()
    // .buildSessionFactory();
    sessionFactory = buildSessionFactory();
    } @AfterClass
    public static void afterClass()
    {
    sessionFactory.close();
    } @Test
    public void testStudentSave()
    {
    StudentPK pk = new StudentPK(); // pk.setId(1); pk.setName("MyNames"); Student s = new Student();
    s.setPk(pk);
    s.setAge(8);
    s.setGood(true); Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.save(s);
    session.getTransaction().commit();
    } /*@Test
    public void testTeacherSave()
    {
    Teacher t = new Teacher();
    t.setId(1);
    t.setName("t1");
    t.setTitle("middle");
    t.setBirthDate(new Date()); Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.save(t);
    session.getTransaction().commit();
    }*/ public static void main(String[] args)
    {
    beforeClass();
    }
    }
      

  4.   

    空指针问题啊。你的那个generator class怎么是uuid,貌似不记得有这个东西啊,你改成native之类常见的试试
      

  5.   

    可能是配置映射文件 XXX.hbm.xml 的问题主键没有生成,如果主键不是程序生成的,主键那的,generator 属性可以写成 native。
    如:<id name="id" type="java.lang.Integer">
          <column name="id" />
          <generator class="native"/>
    </id>
      

  6.   

    ID如果是自动生成的,看看数据库表中的ID属性,选上自动递增。
      

  7.   

    <class name="com.bjsxt.hibernate.model.Student" table="对应数据库的表名">
    <id name="id" type="string">
    <column name="这里是对应数据库里边的字段" length="38" />
    <generator class="uuid" />
    </id>
      

  8.   

    at com.bjsxt.hibernate.model.HibernateIDTest.afterClass(HibernateIDTest.java:47)
    注意这句话是你错误的地方,HibernateIDTest这个类里边的afterClass这个方法报的错,看看你的sessionFactory是不是null,估计就是你没有获得到sessionFactory
      

  9.   

    <class name="com.bjsxt.hibernate.model.Student">
    <id name="id">
    <generator class="uuid"></generator>
    </id>
      

  10.   

    <id name="id">
    <generator class="uuid"></generator>
    </id>你是这样配置的话 你的数据库的里的ID字段必须最少为32位的varchar类型,而且那个自增的对勾也得去掉
    如果不是太需要UUID的话 可改为下面的id配置
    <id name="id" type="java.lang.Integer">
          <column name="id" />
          <generator class="native"/>
    </id>