java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus
<p>In my JAX-RS resource I can inject DBOberationImpl EJB. But in DBOperationImpl bean I can't inject JPA EntityManager. How is this caused and how can I solve it?</p>

<p>Project structure:</p>

<p><a href="https://i.stack.imgur.com/SndOd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SndOd.jpg" alt="enter image description here"></a></p>

<p>EJB interface:</p>

<pre><code>package jpahibernate.service;

import entity.BookmatePromoCode;
import java.util.List;
import javax.ejb.Local;

@Local
public interface DBOperationInterface {

    public List&lt;BookmatePromoCode&gt; bookmatePromoCodes();

}
</code></pre>

<p>EJB implementation:</p>

<pre><code>package jpahibernate.impl;

import entity.BookmatePromoCode;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jpahibernate.service.DBOperationInterface;

@Stateless
@LocalBean
public class DBOperationImpl implements DBOperationInterface {

    @PersistenceContext(unitName = "mypdu")
    private EntityManager em;

    @Override
    public List&lt;BookmatePromoCode&gt; bookmatePromoCodes() {
        System.out.println("mesaj bookmatePromoCodes");
        System.out.println(em);
        Query query = em.createQuery("select b from BookmatePromoCode b");
        List result = query.getResultList();
        System.out.println(result);
        return new ArrayList&lt;BookmatePromoCode&gt;();
    }
}
</code></pre>

<p>JAX-RS resource:</p>

<pre><code>package restfulservices;

import dto.BookmatePromoCodeDTO;
import dto.CustomObjectDTO;
import dto.ErrorDTO;
import dto.ResponseDTO;
import entity.BookmatePromoCode;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless; 
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import jpahibernate.service.DBOperationInterface; 
@Stateless
@Path("/bookmates")
public class BookmateRestfulService {

@EJB
private DBOperationInterface dbOperation;

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/json")
public Response bookmates() { 

    ResponseDTO resp = new ResponseDTO();
    try {
        List&lt;BookmatePromoCode&gt; bookmatePromoCodes = dbOperation.bookmatePromoCodes();
        ArrayList&lt;BookmatePromoCodeDTO&gt; bookmatePromoCodeDTOs = new ArrayList&lt;BookmatePromoCodeDTO&gt;();
        for (BookmatePromoCode bpc : bookmatePromoCodes) {
            //data transfer object
            BookmatePromoCodeDTO bpcDTO = new BookmatePromoCodeDTO();
            bpcDTO.setKod(bpc.getCode());
            bookmatePromoCodeDTOs.add(bpcDTO);
        }
        resp.setObject(new CustomObjectDTO(bookmatePromoCodeDTOs));
    } catch (Exception ex) {
            ex.printStackTrace();
            resp.setError(new ErrorDTO(1, "select zamani xeta bash verdi"));
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(resp).build();
    }
        return Response.ok(resp).build();
    } 
}
</code></pre>

<p>persistence.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"&gt;
    &lt;persistence-unit name="mypdu" transaction-type="JTA"&gt;
        &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
        &lt;class&gt;entity.BookmatePromoCode&lt;/class&gt;
        &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
        &lt;shared-cache-mode&gt;NONE&lt;/shared-cache-mode&gt;
        &lt;properties&gt;
            &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bookmate?characterEncoding=UTF-8&amp;amp;characterSetResults=UTF-8"/&gt;
            &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
            &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
            &lt;property name="javax.persistence.jdbc.password" value="12345"/&gt;
            &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;
        &lt;/properties&gt;
    &lt;/persistence-unit&gt;
&lt;/persistence&gt;
</code></pre>

<p>I can't inject EntityManager. I just get NullPointerException. It couldn't find unitName etc. When I deploy application I get below error:</p>

<pre><code>Warning: Exception while dispatching an event
java.lang.NullPointerException
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
    at org.hibernate.jpa.internal.EntityManagerImpl.&lt;init&gt;(EntityManagerImpl.java:91)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
    at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)
    at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
    at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:492)
    at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:398)
    at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:487)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
</code></pre>