消息关闭
    暂无新消息!

//测试类
public class test extends BaseDaoImpl<Question> {
@Test
public void fun1(){
Question q=new Question();
q.setQst_id(5);
q.setQst_A("dasdsadas");
System.out.println(q.toString());
super.save(q);
}
}


异常:
java.lang.NullPointerException
at com.itheima.bos.dao.base.impl.BaseDaoImpl.save(BaseDaoImpl.java:38)
at test.fun1(test.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)





public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> {
//代表的是某个实体的类型
private Class<T> entityClass;
@Resource//根据类型注入spring工厂中的会话工厂对象sessionFactory
public void setMySessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}

//在父类(BaseDaoImpl)的构造方法中动态获得entityClass
public BaseDaoImpl() {
ParameterizedType superclass = (ParameterizedType) this.getClass().getGenericSuperclass();
//获得父类上声明的泛型数组
Type[] actualTypeArguments = superclass.getActualTypeArguments();
entityClass = (Class<T>) actualTypeArguments[0];
}
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}

public void delete(T entity) {
this.getHibernateTemplate().delete(entity);
}

spring配置hibernate

<!-- 配置LocalSessionFactoryBean,spring提供的用于整合hibernate的工厂bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 注入hibernate相关的属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 注入hibernate的映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath:com/itheima/bos/domain/*.xml</value>
</list>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

实体类

package cn.itcast.domain;

import java.util.HashSet;
import java.util.Set;

public class Question {
   private int qst_id;
   private String qst_name;//题目名称
   private String qst_type;//题目类型
   private String qst_score;//题目分值
   private String qst_A;//A选项
   private String qst_B;//B选项
   private String qst_C;//C选项
   private String qst_D;//D选项
   private String qst_answer;//答案
   private Set<Paper> paper=new HashSet<>();


@Override
public String toString() {
return "Question [qst_id=" + qst_id + ", qst_name=" + qst_name + ", qst_type=" + qst_type + ", qst_score="
+ qst_score + ", qst_A=" + qst_A + ", qst_B=" + qst_B + ", qst_C=" + qst_C + ", qst_D=" + qst_D
+ ", qst_answer=" + qst_answer + ", paper=" + paper + "]";
}
public Question() {
super();
// TODO Auto-generated constructor stub
}
public Question( String qst_name, String qst_type, String qst_score, String qst_A, String qst_B,
String qst_C, String qst_D, String qst_answer) {
super();

this.qst_name = qst_name;
this.qst_type = qst_type;
this.qst_score = qst_score;
this.qst_A = qst_A;
this.qst_B = qst_B;
this.qst_C = qst_C;
this.qst_D = qst_D;
this.qst_answer = qst_answer;

}
public int getQst_id() {
return qst_id;
}
public void setQst_id(int qst_id) {
this.qst_id = qst_id;
}
public String getQst_name() {
return qst_name;
}
public void setQst_name(String qst_name) {
this.qst_name = qst_name;
}
public String getQst_type() {
return qst_type;
}
public void setQst_type(String qst_type) {
this.qst_type = qst_type;
}
public String getQst_score() {
return qst_score;
}
public void setQst_score(String qst_score) {
this.qst_score = qst_score;
}
public String getQst_A() {
return qst_A;
}
public void setQst_A(String qst_A) {
this.qst_A = qst_A;
}
public String getQst_B() {
return qst_B;
}
public void setQst_B(String qst_B) {
this.qst_B = qst_B;
}
public String getQst_C() {
return qst_C;
}
public void setQst_C(String qst_C) {
this.qst_C = qst_C;
}
public String getQst_D() {
return qst_D;
}
public void setQst_D(String qst_D) {
this.qst_D = qst_D;
}
public String getQst_answer() {
return qst_answer;
}
public void setQst_answer(String qst_answer) {
this.qst_answer = qst_answer;
}
public Set<Paper> getPaper() {
return paper;
}
public void setPaper(Set<Paper> paper) {
this.paper = paper;
}
   
}


hibernate

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  
<hibernate-mapping package="cn.itcast.domain" >
<class name="Question" table="exam_question" >

<id name="qst_id"  type="java.lang.Integer">
<!-- generator:主键生成策略(明天讲) -->
<generator class="native"></generator>
</id>

<property name="qst_name"  >
</property>
<property name="qst_type"  ></property>
<property name="qst_score"  ></property>
<property name="qst_A"  ></property>
<property name="qst_B" ></property>
<property name="qst_C"  ></property>
<property name="qst_D"  ></property>
<property name="qst_answer"  ></property>
<!-- 多对多 -->
<set name="paper" table="exam_paper_question">
   <key column="qst_id"></key>
   <many-to-many class="Paper" column="pap_id"></many-to-many>
</set>
</class>
</hibernate-mapping>

折磨我快一天了。。不知道哪里出错了

3个回答

︿ 2
当然空指针了,你用了spring容器,必须用spring的测试包要不然你测试的实例不是spring容器中的当然空指针