刚学hibernate,第一个实例是视频里面的,我都按着操作来的,但是我就是找不到问题出在哪里。异常:java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:330)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:296)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:300)
at StudentTest.main(StudentTest.java:17)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:608)
... 4 morehibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/spring</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySqlDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>student.javapackage com.model;public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}Student.hbm.xml<?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 package="com.model">
<class name="Student" table="student">
<id name="id"></id>
<property name="name" type="String"></property>
<property name="age" type="int"></property>
</class>
</hibernate-mapping>
测试类StudentTest.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import com.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student student=new Student();
student.setId(1);
student.setName("mlwise");
student.setAge(22);
try {
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
s.save(student);
s.getTransaction().commit();
s.close();
sf.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("这里出了问题");
}
catch (Throwable e) {
e.printStackTrace();
}
}
}请各位大虾帮忙,谢谢!!
at org.hibernate.cfg.Configuration.reset(Configuration.java:330)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:296)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:300)
at StudentTest.main(StudentTest.java:17)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:608)
... 4 morehibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/spring</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySqlDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>student.javapackage com.model;public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}Student.hbm.xml<?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 package="com.model">
<class name="Student" table="student">
<id name="id"></id>
<property name="name" type="String"></property>
<property name="age" type="int"></property>
</class>
</hibernate-mapping>
测试类StudentTest.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import com.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student student=new Student();
student.setId(1);
student.setName("mlwise");
student.setAge(22);
try {
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
s.save(student);
s.getTransaction().commit();
s.close();
sf.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("这里出了问题");
}
catch (Throwable e) {
e.printStackTrace();
}
}
}请各位大虾帮忙,谢谢!!
解决方案 »
- servlet验证登录……
- 跪求珠心算源码(java语言)
- struts 2.0.11与struts 2.1.8.1的区别
- struts2 验证码报错 org.apache.catalina.core.StandardWrapperValve invoke - Java / Web
- java怎么远程连接oracle连接库????
- 请问如何把汉字转换成RFC1738编码?
- 求jsp分页代码
- java调用Orace存贮过程返回游标性能问题
- 急急 关于jsp网站中页面不动大概一个小时就必须重运行的原因?
- Vector类在JSP页面中使用出现的问题,请指教。
- Structs开发,如何获取jsp页面的复选框值。。。急。。。
- 求可编辑的下拉框
把int改成integer看看
SessionFactory sf=cfg.configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
s.save(student);
s.getTransaction().commit();
s.close();
sf.close();这个单步调试试试,从try里的第一行代码打断点,开始,看执行到哪行,跳异常了
看到底执行到哪行出问题,就可以定位了
这行你检查下问题
Caused by: java.lang.NullPointerException
private Integer id;
private String name;
private Integer age; 这个地方以后 不要用 java 的包装类型。。这样会导致你的xml文件中。的 类型 不对。。用int 。。你再试试。。我估计是这个地方的问题
那行应该没错,我在网上看到好多例子都是这么写的,我配置hibernate的时候用的是默认文件名。
.configure("catdb.cfg.xml")
.buildSessionFactory();
指定了hibernate的配置文件也是一样的异常,网上说hibernate.cfg.xml是默认的,只要是默认就无需指定,所以我使用了.configure()方法,而没有使用.configure("hibernate.cfg.xml")
我是放在src下,我还尝试过放在WEB-INF目录下,基本可以放的地方都试过了。就是不行。我用的是3.6的hibernate 和slf4 1.6.1