【问题标题】:Caused by: java.lang.NullPointerException at org.o7planning.impl.EmployeDAOImpl.<init>(EmployeDAOImpl.java:34)原因:org.o7planning.impl.EmployeDAOImpl.<init>(EmployeDAOImpl.java:34) 处的 java.lang.NullPointerException
【发布时间】:2017-02-10 19:18:33
【问题描述】:

这是我尝试实例化我的 bean 的 file.XML: applicationDaoContext.XML

`
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

 <bean id="EmployeDAO" class="org.o7planning.impl.EmployeDAOImpl">
     <property name="sessionFactory" ref="SessionFactory" />
 </bean>
</beans>  `

这是我的类 EmployeDaooImpl 未能实例化:( 使用eclipse休眠spring maven tomcat服务器)

package org.o7planning.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.o7planning.dao.EmployeDao;
import org.o7planning.Entity.Employe;
import org.hibernate.Query;

import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class EmployeDAOImpl implements EmployeDao {

    public EmployeDAOImpl() {
        super();


    }

        public void init(){
            new EmployeDAOImpl();
            System.out.println(" mossab method initi de la classe employimpldao");
        }
    private SessionFactory sessionFactory;

    Session session = sessionFactory.getCurrentSession();

            // methode implementer par l'interface
            @SuppressWarnings("unchecked")
            public List<Employe> getAllEmploye() {

                Session session = this.sessionFactory.getCurrentSession();

                List<Employe> list = session.createQuery("from EMPLOYEES").getResultList()  ;
                session.close();
                return list;
}
                // method implementer par l'interface   
            public Integer getMaxEmployeByID() {
                Session session = this.sessionFactory.getCurrentSession();
                String sql = "Select max(E.EMPLOYEE_ID) from EMPLOYEES E ";
                Query query = session.createQuery(sql);
                Integer maxEmployId = (Integer) query.uniqueResult();
                  if (maxEmployId == null) {
                      return 0;
                  }

                  return maxEmployId;
            }

                // methode implementer par l'interface
            public void createEmploy(String nom, String prenom) {

          Integer employId = getMaxEmployeByID() + 1;
          Employe employ = new Employe();
          employ.setId_Employe(employId);
          employ.setNom(nom);
          employ.setPrenom(prenom);
          Session session = this.sessionFactory.getCurrentSession();
          session.persist(employ);

    }

            public SessionFactory getSessionFactory() {
                return sessionFactory;
            }

        @Autowired  
        public void setSessionFactory(SessionFactory sessionFactory) {
                  this.sessionFactory = sessionFactory;
              }

}

【问题讨论】:

  • 谢谢你,保罗,你的信息。
  • 但是我在我的 Beans 中使用了一个 init 方法。我在我的 java 类中创建了这个方法,但同样的异常仍然存在
  • 您的#init() 方法不清楚您要完成什么。请重新格式化代码,以便我们提供帮助。另外,你为什么要在 #init() 中创建一个新的类实例?
  • /@Bean(initMethod="init") public EmployeDAOImpl creerInstance(){ return new EmployeDAOImpl(); }

标签: spring hibernate maven


【解决方案1】:

你需要修改如下代码:

private SessionFactory sessionFactory;
// Remove this line:
// Session session = sessionFactory.getCurrentSession();

此时sessionFactory没有赋值,所以为null。当 Spring 向它注入值时,它的值不为空。 Spring 会调用 setSessionFactory 方法给 sessionFactory 字段注入值。

你可以在 setSessionFactory 方法中编写更多代码。

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 2010-09-10
    • 2014-10-12
    • 1970-01-01
    • 2018-01-04
    • 2013-03-20
    • 2014-02-24
    • 2020-07-03
    相关资源
    最近更新 更多