Stack Exchange
java.lang.NullPointerException in EntityManager with spring mvc and hibernate
I am beginning with Spring MVC and Hibernate.
The system deploy in the server  correctly,  but I have the following exception:
Advertencia:   StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw exception
java.lang.NullPointerException
    at modelo.RefaccionesDAO.consultarTodo(RefaccionesDAO.java:30)
    at modelo.RefaccionesDAO$$FastClassByCGLIB$$b57106b2.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:710)

...

This happen when  this code is execute:
@Service //ESTE ES  A FUERZAS.
public class RefaccionesDAO {

    @PersistenceContext //LAS OPERACIONES POR DEFECTO. 
    private EntityManager em;

    //CONTROLA LAS EXEPCIONES. 
    @Transactional(rollbackFor = {ServicioException.class})
    public Refaccion consultarTodo(){
        em.find(Refaccion.class, "10");
        return new Refaccion(1000, "Rafael Angel", 0, 0, "xdafj", "sldfljdf", true);
    }
}

The especific line is this: em.find(Refaccion.class, "10"); 
em is separated from the return for debugging purpose, because i wanted discar an error in Refaccion
My configurations

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="UNIDAD_DE_PERSISTENCIA_PU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:app/BASE_DE_DATOS_PRUEBAS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>


aplicationContext.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"

       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd

       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-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
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-4.0.1.xsd">

    <jee:jndi-lookup id="myEmf" jndi-name="persistence/UNIDAD_DE_PERSISTENCIA_PU" />

<!--    //PERMITE LAS TRANSACCIONES JPA.--> 
    <tx:jta-transaction-manager />


    <context:component-scan base-package="controller" />
    <context:component-scan base-package="entidades" />
    <context:component-scan base-package="modelo" />


</beans>


dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/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
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <tx:annotation-driven /> <!-- Esto es por que vamos a usar MVC de SPRING -->
    <mvc:annotation-driven/> <!-- Nos permite trabajar con transacciones JPA -->
    <context:annotation-config /> 


    <!-- Esto escanea los paquetes con los que vamos a trabajar. -->
    <context:component-scan base-package="controller" />
    <context:component-scan base-package="entidades" />
    <context:component-scan base-package="modelo" />


    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <mvc:view-controller path="/otro.htm" view-name="otro"/>  

<!--    <mvc:resources  location="/" mapping="/static/**" />-->

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>


Here is where I calling mi RefaccionDao
@Controller
public class TaponesController {


    @Autowired
    private RefaccionesDAO dao;


    @RequestMapping(value = "/tapones/guardar", method = RequestMethod.POST)
    @ResponseBody
    public String guardarTapon(@RequestParam HashMap<String, String> datos, ModelMap model){

        String text = " testo normal xP : ";
        Refaccion r= dao.consultarTodo();

        text = text +" "+ r.getNombre();

        return text;
    }


}


WHAT HAVE I  TRIED?

Change this @PersistenceContext for this:
    @PersistenceContext(unitName = "UNIDAD_DE_PERSISTENCIA_PU", type = PersistenceContextType.EXTENDED)
    private EntityManager em;


Change this : @PersistenceContext(unitName = "UNIDAD_DE_PERSISTENCIA_PU", type = PersistenceContextType.EXTENDED) for this:
    @PersistenceContext(unitName = "UNIDAD_DE_PERSISTENCIA_PU")
    private EntityManager em;

Nothing has changed.

Your entitymanager is not injected. If I were you, I'll use Spring Data for simplicity. You add hibernate as a tag but you use org.eclipse.persistence.jpa.PersistenceProvider as JPA implementation. So ??

@akuma8 I been suppose that I use hibernate. It's my first project and I don´t understand to much. I changed that line for org.hibernate.ejb.HibernatePersistence but now I have a new error: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method Grave:   java.lang.RuntimeException: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence

I solved the problem. I add this to pom.xml
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
</dependency>

Now is working.
