这是student实体类package com.bjsxt.hibernate.model;public class Student {
 private int id;
 private String name;
 private int age;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}这是hibernate.cfg.xml<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
 <session-factory name="foo">
  <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName="stuent"</property>
  <property name="connection.username">sa</property>
  <property name="connection.password">liang925</property>
  <property name="dialect">
   org.hibernate.dialect.SQLServerDialect
  </property>
  <property name="current_session_context_class">thread</property>
  <property name="show_sql">true</property>
  <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>
 </session-factory>
 
</hibernate-configuration>这是hibernate的 Student.hbm.xml<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.bjsxt.hibernate.model.Student" table="student">
  <id name="id"  type="java.lang.Integer">
  <column name="id"></column>
   
  </id>
  <property name="name" type="java.lang.String">
   <column name="name" length="50" not-null="true"/>
  </property>
  <property name="age" type="java.lang.Integer">
   <column name="age" length="50" not-null="true"/>
  </property>
 </class>
</hibernate-mapping>
这是测试类package test;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import com.bjsxt.hibernate.model.Student;public class Test {
 public static void main(String[] args) {
  Student student = new Student();
  student.setAge(1);
  student.setName("小孩");
  Configuration con = null;
  SessionFactory sessionfactory = null;
  Session session = null;
  Transaction ts = null;
  try{
   session = new Configuration().configure().buildSessionFactory().openSession();
   session.save(student);
   ts.commit();
  }catch(HibernateException e){
   ts.rollback();
   e.printStackTrace();
  }finally{
   session.close();
   sessionfactory.close();
  }
 }
}
报的错误是Exception in thread "main" java.lang.NullPointerException
 at test.Test.main(Test.java:23)  //就是session.close();

解决方案 »

  1.   

    Transaction ts = null;
     后没赋值
    这种错误在IDE里面一眼就看到23行是哪里,在这里问一般人没空帮你数23行是哪里
    还好是23行,要是230行怎办
      

  2.   

     Transaction ts = null;没有赋值....
    可以改正这样
    Transaction ts = null;
      try{
      session = new Configuration().configure().buildSessionFactory().openSession();
    //就加上下面这句应该就OK了  ts = session.beginTransaction();//除了查找以外,增删改都要打开事物  session.save(student);
      ts.commit();