title: java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager error in Spring MVC while pulling data
body: <p>I am trying to learn creating ReST API end points using spring mvc and Hibernate without using springboot. When I am running my end point , I am getting internal server error saying that ,</p>
  <p><code>java.lang.NullPointerException: Cannot invoke &quot;javax.persistence.EntityManager.createQuery(String, java.lang.Class)&quot; because &quot;this.entityManager&quot; is null</code>.</p>
  <p>My spring-servlet.xml file under src/main/webapp/web-1nf is like the following,</p>
     
  <pre><code>&lt;beans:beans xmlns=&quot;http://www.springframework.org/schema/mvc&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
  xmlns:beans=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:context=&quot;http://www.springframework.org/schema/context&quot; 
  xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
  xsi:schemaLocation=&quot;http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc.xsd
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd&quot;&gt;
  
  &lt;annotation-driven /&gt;
  
  &lt;resources mapping=&quot;/resources/**&quot; location=&quot;/resources/&quot; /&gt;
  
  &lt;beans:bean id=&quot;dataSource&quot;
      class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;
      &lt;beans:property name=&quot;driverClassName&quot; value=&quot;org.postgresql.Driver&quot; /&gt;
      &lt;beans:property name=&quot;url&quot; value=&quot;jdbc:postgresql://localhost:5432/company&quot; /&gt;
      &lt;beans:property name=&quot;username&quot; value=&quot;postgres&quot; /&gt;
      &lt;beans:property name=&quot;password&quot; value=&quot;postgresql&quot; /&gt;
      &lt;!--&lt;property name=&quot;socketTimeout&quot; value=&quot;10&quot;/&gt;--&gt;
  
      &lt;beans:property name=&quot;connectionProperties&quot;&gt;
          &lt;beans:props&gt;
              &lt;beans:prop key=&quot;socketTimeout&quot;&gt;10&lt;/beans:prop&gt;
          &lt;/beans:props&gt;
      &lt;/beans:property&gt;
   &lt;/beans:bean&gt;
  
  
  &lt;beans:bean id=&quot;hibernate4AnnotatedSessionFactory&quot;
  class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt;
   &lt;beans:property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
   &lt;beans:property name=&quot;annotatedClasses&quot;&gt;
   &lt;beans:list&gt;
   &lt;beans:value&gt;com.springmvc.Employee&lt;/beans:value&gt;
  &lt;/beans:list&gt;
  &lt;/beans:property&gt;
  
  &lt;beans:property name=&quot;hibernateProperties&quot;&gt;
   &lt;beans:props&gt;
     &lt;beans:prop 
       key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.PostgreSQLDialect
     &lt;/beans:prop&gt;
    &lt;beans:prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;/beans:prop&gt;
   &lt;/beans:props&gt;
  &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  
  &lt;context:component-scan base-package=&quot;com.springmvc&quot; /&gt;
  
  
  &lt;tx:annotation-driven transaction-manager=&quot;transactionMgr&quot;/&gt;
  
   &lt;beans:bean id=&quot;transactionMgr&quot; 
    class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
     &lt;beans:property name=&quot;entityManagerFactory&quot; ref=&quot;mgrFactory&quot;/&gt;
   &lt;/beans:bean&gt;
  
     &lt;beans:bean id=&quot;mgrFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
     &lt;beans:property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;
     &lt;beans:property name=&quot;packagesToScan&quot; value=&quot;com.springmvc&quot;/&gt;
      &lt;beans:property name=&quot;jpaVendorAdapter&quot;&gt;
        &lt;beans:bean class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot;/&gt;
     &lt;/beans:property&gt;
     &lt;beans:property name=&quot;jpaProperties&quot;&gt;
        &lt;beans:props&gt;
          &lt;beans:prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;${hibernate.hbm2ddl.auto}&lt;/beans:prop&gt;
          &lt;beans:prop key=&quot;hibernate.dialect&quot;&gt;${hibernate.dialect}&lt;/beans:prop&gt;
          &lt;beans:prop key=&quot;hibernate.cache.use_second_level_cache&quot;&gt;${hibernate.cache.use_second_level_cache}&lt;/beans:prop&gt;
          &lt;beans:prop key=&quot;hibernate.cache.use_query_cache&quot;&gt;${hibernate.cache.use_query_cache}&lt;/beans:prop&gt;
          &lt;beans:prop key=&quot;hibernate.show_sql&quot;&gt;${hibernate.show_sql}&lt;/beans:prop&gt;
          &lt;beans:prop key=&quot;hibernate.format_sql&quot;&gt;${hibernate.format_sql}&lt;/beans:prop&gt;
      &lt;/beans:props&gt;
     &lt;/beans:property&gt;
   &lt;/beans:bean&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  <p>My controller is ,</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/mvchibernate&quot;)
  public class CompanyController {
  
  @Autowired
  EmployeeService employeeService;
  
   @GetMapping(value = &quot;/getAllEmployees&quot;)
      public List&lt;Employee&gt; getEmployeesList() {
       
       @SuppressWarnings(&quot;unchecked&quot;)
      List&lt;Employee&gt; listOfEmployees = employeeService.getAllEmployees();
          return listOfEmployees;
      }
  }
  </code></pre>
  <p>And my service class like the following,</p>
  <pre><code>@Service
  public class EmployeeService {
  
  @Autowired
  EmployeeDAO employeeDaoObj;
  
  public List getAllEmployees() { 
      return employeeDaoObj.getAllEmployees();
    }
  }
  </code></pre>
  <p>And DAO implementation ,</p>
  <pre><code>@Repository
  public class EmployeeDAO {
  
  @PersistenceContext private EntityManager entityManager;
  
  public List&lt;Employee&gt; getAllEmployees() {
      
      String jpql = &quot;SELECT e FROM Employee e&quot;;
      TypedQuery&lt;Employee&gt; query = entityManager.createQuery(jpql, Employee.class);
      
         return query.getResultList();
      
          }
  }
  </code></pre>
  <p>Here in my DAO class I autowired the entitymanager.And while running I am getting <code>Cannot invoke javax.persistence.EntityManager</code> and last showing that <code>because &quot;this.entityManager&quot; is null</code>.</p>
  <p>So can anyone guide me to resolve this issue or kindly refer any documentation to follow please?</p>