0

I have this simple EJB class. When I try to inject EntityManager instance I get a NullPointerException:

Caused by: java.lang.NullPointerException
at pl.take.server.model.WholesaleEJB.createClient(WholesaleEJB.java:54) [:]

The question is what am I doing wrong and how can I fix it? I am using netbeans and jboss 6.1. Thank you for your time.

@Stateless
public class WholesaleEJB {

    @PersistenceContext(unitName = "wholesale")
    EntityManager entityManager;

    public void createClient(Clients client) {
        entityManager.persist(client);            //Error line
    }

}

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="wholesale" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/Wholesale</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
   <property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

EDIT:

Class REST with instance of WholesaleEJB:

@Stateless
@Path("/wholesale")
public class WholesaleREST{

    @EJB
    private WholesaleEJB wholesaleEJB = new WholesaleEJB();

    @POST
    @Path("/create")
    public String createClient(InputStream is) {
        Clients clients = JAXB.unmarshal(is, Clients.class);
        wholesaleEJB.createClient(clients);
        return "Client created!";
    }
}

Full error:

00:31:56,458 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/WholesaleApp].[default]] Servlet.service() for servlet default threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    at org.jboss.resteasy.core.SynchronousDispatcher.unwrapException(SynchronousDispatcher.java:345) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:321) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:534) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.invokePropagateNotFound(SynchronousDispatcher.java:155) [:6.1.0.Final]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:212) [:6.1.0.Final]
    at org.jboss.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:59) [:6.1.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.1.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]
    at java.lang.Thread.run(Thread.java:745) [:1.8.0_51]
Caused by: java.lang.NullPointerException
    at pl.take.server.model.WholesaleEJB.createClient(WholesaleEJB.java:48) [:]
    at pl.take.server.model.WholesaleREST.createClient(WholesaleREST.java:57) [:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.8.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [:1.8.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.8.0_51]
    at java.lang.reflect.Method.invoke(Method.java:497) [:1.8.0_51]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140) [:6.1.0.Final]
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:255) [:6.1.0.Final]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:220) [:6.1.0.Final]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:209) [:6.1.0.Final]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:519) [:6.1.0.Final]
    ... 23 more

Full log from deploing project on server: http://pastebin.com/Ki3Gz4RA

Demento
  • 4,039
  • 3
  • 26
  • 36
pawkondr
  • 117
  • 1
  • 3
  • 16

2 Answers2

0

This is your problem:

@EJB
private WholesaleEJB wholesaleEJB = new WholesaleEJB();

By calling the constructor, you're building a POJO for wholesaleEJB. EJB works by allowing the container to create a remote proxy which wraps the EJB services (including dependency injection) around the POJO.

Replace this with:

@EJB
private WholesaleEJB wholesaleEJB;

Then the container will read the annotation, inject a copy of the EJB, and have it ready for you to call. If you want to control the process yourself (you shouldn't really) you can use a JNDI lookup to get the EJB object, rather than calling the constructor directly.

hugh
  • 2,237
  • 1
  • 12
  • 25
  • I did it. Now i have: `Caused by: java.lang.NullPointerException at pl.take.server.model.WholesaleREST.createClient(WholesaleREST.java:32) [:]` – pawkondr Sep 19 '15 at 11:48
  • Ok, that problem shows the dependency injection is not working. Are you running on JBoss when you get that NPE? Do you see a message like the first line here: https://developer.jboss.org/thread/233440#842013 about WholesaleREST? That would tell us if JBoss is finding and deploying the bean correctly. – hugh Sep 19 '15 at 12:06
  • Deploing is succesful. I got NPE when i call method from my client. Then server throws error from post. Also I added full log from deploy to post but i found interesting line: `14:09:23,616 WARN [org.hibernate.ejb.Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec correctly.PersistenceUnitInfo.getNewTempClassLoader() is null.` Is it important? – pawkondr Sep 19 '15 at 12:24
  • Thanks. Are the two beans deployed in the same WAR? Could you try adding this method to WholesaleREST: http://pastebin.com/29tMthe1 – hugh Sep 19 '15 at 12:39
  • Yes, two beans in one war one ejb, one rest. I added method, nothing have changed. Should i call that method in client? – pawkondr Sep 19 '15 at 13:07
  • First could you try debugging to see whether the method is called? (It should be, because of the `PostConstruct`, but sounds like it isn't), then try calling it at the top of `createClient`. (NB: If this works, it still isn't threadsafe) – hugh Sep 19 '15 at 13:13
  • That method `jndiLookupWholesaleEJB()` wasn't called. I called it from method `createClient`. New exception was thrown: `java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to pl.take.server.model.WholesaleEJB` – pawkondr Sep 19 '15 at 13:26
  • Looking into this other issue, this question seems to deal with it quite comprehensively: http://stackoverflow.com/questions/3027834/inject-an-ejb-into-jax-rs-restful-service That gives a couple of other options, one more is (assuming the JNDI lookup works this time), you could take the @Stateless annotation off `WholesaleREST` and put this in a constructor. (Don't put it in the constructor while that annotation is there though - that would break EJB context rules) – hugh Sep 19 '15 at 13:31
  • I pasted yor new code and its still not working i have `java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to pl.take.server.model.WholesaleEJB` why is that happening? Maybe the whole project has bad configuration or sth? I don't have any ideas. – pawkondr Sep 19 '15 at 15:07
  • If your application server is not EJB 3.1 compliant you cannot inject directly the ejb implementation, you need to inject the EJB interface into your REST Service – antoniodvr Sep 21 '15 at 07:02
0

Probably your Application Server is not compliant about EJB 3.1 and you cannot perform the EJB injection in that way.

You could try to create an interface for your EJB with the needed methods and then inject this one in your REST Service.

antoniodvr
  • 1,259
  • 1
  • 14
  • 15