首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请问hibernate中报空指针的有关问题

2012-01-28 
请教hibernate中报空指针的问题本人在看孙的hibernate其中第2章的例子在运行时报错java.lang.NullPointerE

请教hibernate中报空指针的问题
本人在看孙的hibernate   其中第2章的例子   在运行时报错
java.lang.NullPointerException
at   mypack.BusinessService.saveCustomer(BusinessService.java:54)
at   mypack.BusinessService.test(BusinessService.java:187)
at   mypack.BusinessService.main(BusinessService.java:197)
Exception   in   thread   "main "  

其中BusinessService.java如下
public   class   BusinessService   {
    public   static   SessionFactory   sessionFactory;
    static{
        try{
            //   Create   a   configuration   based   on   the   properties   file   we 've   put
            //   in   the   standard   place.
        Configuration   config   =   new   Configuration().configure();
        SessionFactory   sessionFactory   =   config.buildSessionFactory();


            //   Get   the   session   factory   we   can   use   for   persistence
            sessionFactory   =   config.buildSessionFactory();
        }catch(Exception   e){e.printStackTrace();}
    }

    public   void   findAllCustomers(ServletContext   context,OutputStream   out)   throws   Exception{
        //   Ask   for   a   session   using   the   JDBC   information   we 've   configured
        Session   session   =   sessionFactory.openSession();
        Transaction   tx   =   null;
        try   {
            tx   =   session.beginTransaction();
            List   customers=session.find( "from   Customer   as   c   order   by   c.name   asc ");
            for   (Iterator   it   =   customers.iterator();   it.hasNext();)   {
                  printCustomer(context,out,(Customers)   it.next());
            }

            //   We 're   done;   make   our   changes   permanent
            tx.commit();

        }catch   (Exception   e)   {
            if   (tx   !=   null)   {
                //   Something   went   wrong;   discard   all   partial   changes
                tx.rollback();
            }
            throw   e;
        }   finally   {
            //   No   matter   what,   close   the   session
            session.close();
        }
    }
    public   void   saveCustomer(Customers   customer)   throws   Exception{


        //   Ask   for   a   session   using   the   JDBC   information   we 've   configured
        Session   session   =   sessionFactory.openSession();
        Transaction   tx   =   null;
        try   {
            tx   =   session.beginTransaction();
            session.save(customer);
            //   We 're   done;   make   our   changes   permanent
            tx.commit();

        }catch   (Exception   e)   {
            if   (tx   !=   null)   {
                //   Something   went   wrong;   discard   all   partial   changes
                tx.rollback();
            }
            throw   e;
        }   finally   {
            //   No   matter   what,   close   the   session
            session.close();
        }
    }

    public   void   loadAndUpdateCustomer(Long   customer_id,String   address)   throws   Exception{
        //   Ask   for   a   session   using   the   JDBC   information   we 've   configured
        Session   session   =   sessionFactory.openSession();
        Transaction   tx   =   null;
        try   {
            tx   =   session.beginTransaction();

            Customers   c=(Customers)session.load(Customers.class,customer_id);
            c.setAddress(address);
            //   We 're   done;   make   our   changes   permanent
            tx.commit();

        }catch   (Exception   e)   {
            if   (tx   !=   null)   {
                //   Something   went   wrong;   discard   all   partial   changes
                tx.rollback();
            }
            throw   e;
        }   finally   {
            //   No   matter   what,   close   the   session
            session.close();
        }
    }
    public   void   deleteAllCustomers()   throws   Exception{
        //   Ask   for   a   session   using   the   JDBC   information   we 've   configured
        Session   session   =   sessionFactory.openSession();


        Transaction   tx   =   null;
        try   {
            tx   =   session.beginTransaction();
            session.delete( "from   Customer   as   c ");
            //   We 're   done;   make   our   changes   permanent
            tx.commit();

        }catch   (Exception   e)   {
            if   (tx   !=   null)   {
                //   Something   went   wrong;   discard   all   partial   changes
                tx.rollback();
            }
            throw   e;
        }   finally   {
            //   No   matter   what,   close   the   session
            session.close();
        }
    }

    private   void   printCustomer(ServletContext   context,OutputStream   out,Customers   customer)throws   Exception{
    if(out   instanceof   ServletOutputStream)
                      printCustomer(context,(ServletOutputStream)   out,customer);
              else
                    printCustomer((PrintStream)   out,customer);
    }
    private   void   printCustomer(PrintStream   out,Customers   customer)throws   Exception{
      //save   photo
        byte[]   buffer=customer.getImage();
        FileOutputStream   fout=new   FileOutputStream( "photo_copy.gif ");
        fout.write(buffer);
        fout.close();

        out.println( "------以下是 "+customer.getName()+ "的个人信息------ ");
        out.println( "ID:   "+customer.getId());
        out.println( "口令:   "+customer.getPassword());
        out.println( "E-Mail:   "+customer.getEmail());
        out.println( "电话:   "+customer.getPhone());
        out.println( "地址:   "+customer.getAddress());
        String   sex=customer.getSex()== 'M '?   "男 ": "女 ";
        out.println( "性别:   "+sex);
        String   marriedStatus=customer.isMarried()?   "已婚 ": "未婚 ";
        out.println( "婚姻状况:   "+marriedStatus);
        out.println( "生日:   "+customer.getBirthday());
        out.println( "注册时间:   "+customer.getRegisteredTime());
        out.println( "自我介绍:   "+customer.getDescription());

    }
    private   void   printCustomer(ServletContext   context,ServletOutputStream   out,Customers   customer)throws   Exception{


        //save   photo
        byte[]   buffer=customer.getImage();
        String   path=context.getRealPath( "/ ");
        FileOutputStream   fout=new   FileOutputStream(path+ "photo_copy.gif ");
        fout.write(buffer);
        fout.close();

        out.println( "------以下是 "+customer.getName()+ "的个人信息------ "+ " <br> ");
        out.println( "ID:   "+customer.getId()+ " <br> ");
        out.println( "口令:   "+customer.getPassword()+ " <br> ");
        out.println( "E-Mail:   "+customer.getEmail()+ " <br> ");
        out.println( "电话:   "+customer.getPhone()+ " <br> ");
        out.println( "地址:   "+customer.getAddress()+ " <br> ");
        String   sex=customer.getSex()== 'M '?   "男 ": "女 ";
        out.println( "性别:   "+sex+ " <br> ");
        String   marriedStatus=customer.isMarried()?   "已婚 ": "未婚 ";
        out.println( "婚姻状况:   "+marriedStatus+ " <br> ");
        out.println( "生日:   "+customer.getBirthday()+ " <br> ");
        out.println( "注册时间:   "+customer.getRegisteredTime()+ " <br> ");
        out.println( "自我介绍:   "+customer.getDescription()+ " <br> ");
        out.println( " <img   src= 'photo_copy.gif '   border=0> <p> ");
    }
      public   void   test(ServletContext   context,OutputStream   out)   throws   Exception{

        Customers   customer=new   Customers();
        customer.setName( "Tom ");
        customer.setEmail( "tom@yahoo.com ");
        customer.setPassword( "1234 ");
        customer.setPhone(55556666);
        customer.setAddress( "Shanghai ");
        customer.setSex( 'M ');
        customer.setDescription( "I   am   very   honest. ");

        InputStream   in=this.getClass().getResourceAsStream( "photo_copy.gif ");
        byte[]   buffer   =   new   byte[in.available()];
        in.read(buffer);
        customer.setImage(buffer);
        customer.setBirthday(Date.valueOf( "1980-05-06 "));

        saveCustomer(customer);

        findAllCustomers(context,out);
        loadAndUpdateCustomer(customer.getId(), "Beijing ");
        findAllCustomers(context,out);

        deleteAllCustomers();
    }

    public   static   void   main(String   args[])   throws   Exception   {


        new   BusinessService().test(null,System.out);
        sessionFactory.close();
    }

}
工具包是   hibernate3.0的   数据库ORACLE


[解决办法]
可能是那个对象是空的
[解决办法]
saveCustomer()中应有个对象是null;
可以简单的加入些打印语句看哪个对象是空的,如:
public void saveCustomer(Customers customer) throws Exception{
// Ask for a session using the JDBC information we 've configured
System.out.println( "sessionFactory: "+sessionFactory);
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(customer);
// We 're done; make our changes permanent
tx.commit();

}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}

热点排行