EJB in REST service returns null pointer exception

11,914

Solution 1

I believe the cause of this is because you are using RestEasy explicitly. It cannot inject the EJB because it doesn't know about it. Can you try replacing the entire web.xml above with:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>BarBar</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <servlet-mapping>
        <servlet-name>yourcompany.yourpackage.MyRESTApplication</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

(i.e. substituted all the RestEasy stuff with the standard servlet mapping for JAX-RS - see JAX-RS 1.1 specs, ch. 2.3.2; obviously replace yourcompany.yourpackage.MyRESTApplication with the real fully quallified name of MyRESTApplication)

Alternatively, annotate your application class with javax.ws.rs.ApplicationPath and remove all relevant configuration from web.xml:

@ApplicationPath("/*")
public class MyRESTApplication extends Application {
    ...
}

Solution 2

Hints from Nikos are completely true. However it's strange that code works for you. In your code You create reference of TablesResource class "manually". You call constructor explicit.

public MyRESTApplication(){
     singletons.add(new TablesResource());
}

The class MyRESTApplication should be change as follow. The point is to show container which classes it should use. And it has to call constructor(s) itself.

public class MyRESTApplication extends Application {

   private Set<Object> singletons = new HashSet<Object>();
   private Set<Class<?>> classes = new HashSet<Class<?>>();

   public MyRESTApplication(){
        classes.add(TablesResource.class); 
        // singletons.add(new TablesResource());
   }

   @Override
   public Set<Class<?>> getClasses() {
        return classes;
    }

   @Override
   public Set<Object> getSingletons() {
        return singletons;
   }
}

(I had similar problem and I solved it with above changes. The problem is the code automatically generated from Eclipse JBoss Tool "Create Sample RESTful Service".)

Share:
11,914
Adam Bardon
Author by

Adam Bardon

Creator of ioscookies.com - a hand curated collection of iOS libraries written in Swift, presetbrewery.com - a drag-n-drop tool for converting Lightroom presets to Adobe Camera Raw, boxiqq.co - a platform to create beautiful and customizable dashboards

Updated on June 04, 2022

Comments

  • Adam Bardon
    Adam Bardon almost 2 years

    I'm trying to implement REST service with Resteasy and Jboss as7, I have session bean which gets data from DB through DAO(it works on JSF+managed bean), and I want to inject that session bean with @EJB, but it returns null, am I doing something wrong?

    MyRESTApplication.java:

    public class MyRESTApplication extends Application {
    
        private Set<Object> singletons = new HashSet<Object>();
        private Set<Class<?>> empty = new HashSet<Class<?>>();
        public MyRESTApplication(){
             singletons.add(new TablesResource());
        }
        @Override
        public Set<Class<?>> getClasses() {
             return empty;
        }
        @Override
        public Set<Object> getSingletons() {
             return singletons;
        }
    }
    

    TablesResource.java:

    @Path("/RESTService")
    public class TablesResource {
    
        @EJB
        private TablesSB tablesSB;
    
        @GET
        @Produces("application/xml")
        @Path("/table")
        public Tables getTable() {
            return tablesSB.getTable(28);
        }
    }
    

    session bean:

    @Stateless
    @LocalBean
    public class TablesSB {
    
        @PersistenceContext
        private EntityManager em;
    
        private TablesDAO tablesDao;
        private TablesDAOxml tablesDaoXml;
    
        public TablesSB() {
        }
    
        @PostConstruct
        public void init(){
            //tablesDao = new TablesDAO(em);
            tablesDaoXml = new TablesDAOxml();
        }
    
        public Tables getTable(int tableId) {
            return tablesDaoXml.getTable(tableId);
        }
        ...
    }
    

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>BarBar</display-name>
      <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
      <context-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>ws.MyRESTApplication</param-value>
      </context-param>
      <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
      </listener>
    </web-app>
    

    stack trace:

    20:59:06,820 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/BarBar].[Resteasy]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet Resteasy threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
        at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.6.Final.jar:]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
        at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
        at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_45]
    Caused by: java.lang.NullPointerException
        at ws.TablesResource.getTable(TablesResource.java:30) [classes:]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_45]
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.6.Final.jar:]
        ... 19 more