我正在使用Hibernate框架开发Web应用程序。尝试运行webapp时出现此错误。
错误控制台:
Exception caught in Create Account Dataorg.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer] java.lang.NullPointerException
我的映射文件(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="org.vgec.bean.CreateAccount" table="CreateAccount" > <id name="enrollment_number" type="java.lang.String"> <column name="enrollment_number" length="20"/> <generator class="assigned" /> </id> <property name="activation_code" type="java.lang.String"> <column name="activation_code" length="50"/> </property> </class> </hibernate-mapping>
DataAccessObject文件代码:
public void addCreateAccount(CreateAccount act) throws Exception { Session session = null; try{ //this step will read hibernate.cfg.xml SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(act); tx.commit(); }catch(Exception e) { System.out.println("Exception caught in Create Account Data" + e); }finally{ session.flush(); session.close(); } }
在上面的代码中,抛出了异常-在catch中的字符串显示在控制台的错误中。
这是我的bean或POJO文件:
package org.vgec.bean; public class CreateAccount { private String enrollment_number; private String activation_code; public String getEnrollment_number() { return enrollment_number; } public void setEnrollment_number(String enrollment_number) { this.enrollment_number = enrollment_number; } public String getActivation_code() { return activation_code; } public void setActivation_code(String activation_code) { this.activation_code = activation_code; } }
javaassist.jar
java.lang.NullPointerException
此错误的根本原因是什么?
您应该显式定义no-arg构造函数。这是从Hibernate到所有POJO的要求。