title: NullPointerException during initialization of Spring bean
body: <p>I am developing simple Hibernate project with Spring IoC and faced stupid NPE during injection. 
  Both xml and Java style Spring configurations turned out to be inefficient and I stuck. Any help will be appreciated.</p>
  
  <p>Exception I meet:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at com.phoneBook.DAO.ContactDAOImpl.getSession(ContactDAOImpl.java:22)
  at com.phoneBook.DAO.ContactDAOImpl.save(ContactDAOImpl.java:28)
  at com.phoneBook.DAO.ContactDAOImpl.main(ContactDAOImpl.java:61)
  </code></pre>
  
  <p>My application-config.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                              http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.phoneBook" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location" value="classpath:properties/database.properties" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
          &lt;property name="url" value="${jdbc.url}" /&gt;
          &lt;property name="username" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="com.phoneBook.entities" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.flushMode"&gt;${hibernate.flushMode}&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>application-context.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.phoneBook" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Contact.class</p>
  
  <pre><code>package com.phoneBook.entities;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="contacts")
  public class Contact {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="contact_id")
  private int id;
  
  @Column(name="contact_surname", nullable = false, length = 20)
  private String surname;
  
  @Column(name="contact_name", nullable = false, length = 20)
  private String name;
  
  @Column(name="contact_patname", nullable = false, length = 20)
  private String patName;
  
  @Column(name="contact_mobtel", unique = true, nullable = false, length = 12)
  private String telNumberMob;
  
  @Column(name="contact_hometel", length = 12)
  private String telNumberHome;
  
  @Column(name="contact_address", length = 30)
  private String address;
  
  @Column(name="contact_email", length = 20)
  private String email;
  
      public Contact(){}
  
      public Contact(String surname, String name, String patName, String mobTel, String homeTel, String address, String email){
          this.surname = surname;
          this.name = name;
          this.patName = patName;
          this.telNumberMob = mobTel;
          this.telNumberHome = homeTel;
          this.address = address;
          this.email = email;
      }
      //getters and setters
  
  }
  </code></pre>
  
  <p>and ContactDAOimpl.java</p>
  
  <pre><code>    package com.phoneBook.DAO;
  
  import java.util.List;
  
  import javax.transaction.Transactional;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import com.phoneBook.entities.Contact;
  
  @Repository
  @Transactional
  public class ContactDAOImpl implements ContactDAO {
      @Autowired
      private SessionFactory sessionFactory;   
  
      protected Session getSession(){
           return sessionFactory.getCurrentSession();
      }
  
      @Override
      public void save(Contact cont) {
          if(cont.getId()==0)
              getSession().persist(cont);
          else
              getSession().update(cont);
      }
  
      @Override
      public Contact getById(int id) {
          Contact contact = (Contact)getSession().createCriteria(Contact.class).add(Restrictions.eq("id",id)).uniqueResult();
            return contact;
      }
  
      @Override
      public List&lt;Contact&gt; getAll() {
          @SuppressWarnings("unchecked")
          List&lt;Contact&gt; allContacts = sessionFactory.getCurrentSession().createCriteria(Contact.class).list();
          return allContacts;
      }
  
      @Override
      public int remove(Contact cont) {
          getSession().delete(cont);
          return cont.getId();
      }
  
      public static void main(String args[]){
          ContactDAOImpl dao = new ContactDAOImpl();
          Contact testContact = new Contact("name", "surname", "patname", "380442322233", "380442322233", 
                  "address", "name@gmail.com");
          dao.save(testContact);
      }
  
  }
  </code></pre>
  
  <p>My dependencies:</p>
  
  <p><a href="https://i.stack.imgur.com/5Zy4m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5Zy4m.png" alt="Dependencies"></a></p>