title: Null Pointer Exception in RestEasy Asynchronous Request
body: <p>I was working in Resteasy where I've to make a asynchronous request to server. The real purpose is, I'll be submitting a form which will be converted into a .xlsx file which will take atleast 10 seconds to complete. So Asynchronous request is the best way here. I followed the procedures from the following link.</p>
  
  <p><a href="https://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/#Asynchronous_HTTP_Request_Processing" rel="nofollow">https://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/#Asynchronous_HTTP_Request_Processing</a></p>
  
  <p>I'm making the ajax request like this. </p>
  
  <pre><code>$.ajax({
      url : 'rest/parentPath/childPath',
      type : 'GET',
      success : function(data, status, xhr) {
          console.log(xhr.getResponseHeader('Location'));
      },
      failure : function(data) {
          console.log(data);
      },
      error : function(error,status) {
  
      }
  });
  </code></pre>
  
  <p>ParentClass.java</p>
  
  <pre><code>import javax.ws.rs.container.AsyncResponse;
  import javax.ws.rs.container.Suspended;
  
  @Component
  @Path("/parentPath")
  public class ParentClass {
      @GET
      @Path("childPath")
      @Produces("text/plain")
      public void asyncFunction(@Suspended final AsyncResponse response){
          Thread t = new Thread() {
              @Override
              public void run()
              {
                  try
                  {
                      Response jaxrs = Response.ok("basic").type(MediaType.TEXT_PLAIN).build();         
                      System.out.println("entered======================= =================================================");
                      response.resume(jaxrs);
                  }
                  catch (Exception e){
                      e.printStackTrace();
                  }
              }
          };
          t.start();
      }
  }
  </code></pre>
  
  <p>If I simply make a ajax request, it gives me <code>503 Service unavailable error</code> but I do get my Asynchronous task executed, I can confirm by seeing my sysout present in wildfly log. But this is not a way how a asynchronous should be done. I've to be able to see the response of my asynchronous task in the second request. I followed the procedures in this link.</p>
  
  <p><a href="https://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/#async_job" rel="nofollow">https://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/#async_job</a></p>
  
  <p>If I put <code>?asynch=true</code> in the request url, immediately i get a response of <code>202 Accepted</code> with a location of asynchronous job in response. But it didn't even entered into the try statement. An error is thrown in the wildfly terminal like this.</p>
  
  <pre><code>19:11:41,733 WARN  [org.jboss.resteasy.core.ExceptionHandler] (pool-4-thread-1) Failed executing GET /parentPath/childPath: org.jboss.resteasy.spi.BadRequestExcept
  ion: Failed processing arguments of org.jboss.resteasy.spi.metadata.ResourceMethod@44d4407c
          at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:104) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:112) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.AsynchronousDispatcher.invokeSuper(AsynchronousDispatcher.java:237) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.AsynchronousDispatcher$1.call(AsynchronousDispatcher.java:278) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.AsynchronousDispatcher$1.call(AsynchronousDispatcher.java:269) [resteasy-jaxrs-3.0.10.Final.jar:]
          at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_25]
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
          at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
  Caused by: java.lang.NullPointerException
          at org.jboss.resteasy.core.ResourceMethodInvoker.initializeAsync(ResourceMethodInvoker.java:374) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.AsynchronousResponseInjector.inject(AsynchronousResponseInjector.java:43) [resteasy-jaxrs-3.0.10.Final.jar:]
          at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:89) [resteasy-jaxrs-3.0.10.Final.jar:]
          ... 12 more
  </code></pre>
  
  <p>If I made the same request again with <code>asynch=true</code>, it shows the same error but with <code>(pool-4-thread-2)</code> instead of <code>(pool-4-thread-1)</code></p>
  
  <p>This means exception is not occured at the server side but at the runtime layer. Coz any exception occured inside my code will be present in log file but not in wildfly terminal. I'll post the web.xml, WebConfig.java, build.gradle files. I'm just replicating the same thing which is done in jboss docs, but I cant figure out why is this exception occuring at the wildfly layer.</p>
  
  <p>Web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt;
  
      &lt;display-name&gt;Web Application&lt;/display-name&gt;
  
      &lt;distributable /&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.jboss.resteasy.plugins.spring.SpringContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Context Configuration locations for Spring XML files --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              classpath:/applicationContext-resources.xml
              classpath:/applicationContext-dao.xml
              classpath:/applicationContext-service.xml
              classpath*:/applicationContext.xml
              /WEB-INF/applicationContext*.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;resteasy.servlet.mapping.prefix&lt;/param-name&gt;
          &lt;param-value&gt;/rest&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;resteasy.async.job.service.enabled&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;resteasy.async.job.service.max.job.results&lt;/param-name&gt;
          &lt;param-value&gt;100&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Maximum wait time on a job when a client is querying for it --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;resteasy.async.job.service.max.wait&lt;/param-name&gt;
          &lt;param-value&gt;300000&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Thread pool size of background threads that run the job --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;resteasy.async.job.service.thread.pool.size&lt;/param-name&gt;
          &lt;param-value&gt;100&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Set the base path for the Job uris --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;resteasy.async.job.service.base.path&lt;/param-name&gt;
          &lt;param-value&gt;/asynch/jobs&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;resteasy-servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
              &lt;param-value&gt;com.mypackage.service.WebConfig&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;resteasy-servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;login.html&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>WebConfig.java</p>
  
  <pre><code>import java.util.HashSet;
  import java.util.Set;
  
  import javax.ws.rs.core.Application;
  
  public class WebConfig extends Application {
  
      private Set&lt;Object&gt; singletons = new HashSet&lt;Object&gt;();
      private Set&lt;Class&lt;?&gt;&gt; empty = new HashSet&lt;Class&lt;?&gt;&gt;();
  
      public WebConfig() {
          // ADD YOUR RESTFUL RESOURCES HERE
          this.singletons.add(new SignupService());
          this.singletons.add(new UserService());
          this.singletons.add(new ParentClass());
      }
  
      public Set&lt;Class&lt;?&gt;&gt; getClasses()
      {
          return this.empty;
      }
  
      public Set&lt;Object&gt; getSingletons()
      {
          return this.singletons;
      }
  
  }
  </code></pre>
  
  <p>Build.gradle</p>
  
  <pre><code>apply plugin: 'java'
  apply plugin: 'war'
  apply plugin: 'eclipse-wtp'
  apply plugin: 'eclipse'
  
  // Uses JDK 8
  sourceCompatibility = 1.8
  targetCompatibility = 1.8
  
  // 1. Get dependencies from Maven local repository
  // 2. Get dependencies from Maven central repository
  repositories {
      mavenLocal()
      mavenCentral()
      maven  {
          url "http://repo1.maven.org/maven2"
      }
  }
  
  configurations {
      provided
  }
    sourceSets {
      main { compileClasspath += configurations.provided }
  }
  
  //Project dependencies
  dependencies {
  
      //Spring framework core
      compile 'org.springframework:spring-web:4.1.4.RELEASE'
      compile 'org.springframework:spring-core:4.1.4.RELEASE'
      compile 'org.springframework:spring-context:4.1.4.RELEASE'
      compile 'org.springframework:spring-context-support:4.1.4.RELEASE'
      compile 'org.springframework:spring-orm:4.1.4.RELEASE'
  
      compile 'org.springframework.security:spring-security-core:4.0.0.RELEASE'
  
      //MySQL database driver
      //compile 'mysql:mysql-connector-java:5.1.34'
      compile 'com.oracle:ojdbc6:11.2.0.1.0'
  
  
      //Hibernate framework 
      compile 'org.hibernate:hibernate-core:4.3.8.Final'
      compile 'commons-dbcp:commons-dbcp:1.2.2'
  
      //Servlet API
      compile 'javax.servlet:servlet-api:2.5'
  
      //Base-64 Apache commons
      compile 'commons-codec:commons-codec:1.10'
  
      //log4j
      compile 'log4j:log4j:1.2.17'
      compile 'org.slf4j:slf4j-simple:1.7.10'
  
      //XmlBeans Equity Valuation
      compile 'org.apache.xmlbeans:xmlbeans:2.6.0'
  
      //Poi Equity Valuation
      compile 'org.apache.poi:poi:3.10.1'
  
      //Poi ooxml Equity Valuation
      compile 'org.apache.poi:poi-ooxml:3.10.1'
  
      //Poi ooxml Schemas Equity Valuation
      compile 'org.apache.poi:poi-ooxml-schemas:3.10.1'
  
      //Jacob Equity Valuation
      compile 'jacob:jacob:1.18-M2'
  
      //Google gson
      compile 'com.google.code.gson:gson:2.3.1'
  
      provided 'org.jboss.resteasy:resteasy-jaxrs:3.0.11.Final'
  
      provided 'org.jboss.resteasy:resteasy-spring:3.0.11.Final'
  
  }
  </code></pre>