我是想插入个数据,但是我的session没取到,抱错为java.lang.NullPointerException
,但我不知道是不是配置文件的错误

解决方案 »

  1.   

    把你的hibernate.cfg.xml传上来看看
      

  2.   

    报的是java.lang.NullPointerException错误说明是你取值抛出的,也就是说你在调用你的 对象方方时,对象是空的。
    从你的 映射文件看没什么问题,看看楼主自己的 HQL语句吧,也许能把问题解决
      

  3.   

    谢谢,但是我查了很久也没问题,我调用代码如下:
    package mypack;import net.sf.hibernate.*;
    import net.sf.hibernate.cfg.Configuration;
    import java.util.*;public class BusinessService {
       public static SessionFactory sessionFactory;
       static{
       try{
      Configuration config=new Configuration().configure();
      config.addClass(Customer.class)
            .addClass(Order.class);
      
      sessionFactory=config.buildSessionFactory();
      }catch(Exception e){e.printStackTrace();}
       }
       //public BusinessService(){}
       public void savedata() throws Exception{
       System.out.println("zhixing");
       
       Session session=sessionFactory.openSession();
      Transaction tx=null;
     
      try{
      tx=session.beginTransaction();
      Customer customer=new Customer("lyy");
      session.save(customer);
      
      Order order1=new Order("tom_001",customer);
      Order order2=new Order("tom_002",customer);
      session.save(order1);
      session.save(order2);
      tx.commit();
      }catch(Exception e){
      if(tx!=null){
      tx.rollback();
      }
      throw e;
      }finally{
      session.close();
      }
      
       }
       public void test() throws Exception{
          savedata();
      }
       public static void main(String args[]) throws Exception{
       new BusinessService().test();
      sessionFactory.close();
       System.out.println("main");
       }
    }
    配置文件用的是hibernate.properties如下:
    hibernate.dialect=net.sf.hibernate.dialect.SybaseDialect
    hibernate.connection.driver_class=com.sybase.jdbc2.jdbc.SybDriver
    hibernate.connection.url=jdbc:sybase:Tds:192.168.0.201:5000/weihu
    hibernate.connection.username=sa
    hibernate.connection.password=111111
    hibernate.show_sql=true我就不清楚到底是什么错??