title: Primefaces JSF null returned from RequestContext.getCurrentInstance()
body: <p>I'm developing an app using Primefaces tag library.</p>
  
  <p>So far I've copied some examples from their official website and all seems to work except for anything that uses the RequestContext.getCurrentInstance() method. It either returns null every time, causing Tomcat to throw a NullPointer exception or hangs forever, never returning anything.</p>
  
  <p>Here's my code (the case where it hangs):</p>
  
  <p>GlobalCounterBean.java</p>
  
  <pre><code>package org.primefaces.examples.view;
  
  import java.io.Serializable;
  import org.primefaces.context.RequestContext;
  
  public class GlobalCounterBean implements Serializable{
  
      private int count;
  
      public int getCount() {
      return count;
      }
  
      public void setCount(int count) {
      this.count = count;
      }
  
      public synchronized void increment() {
      count++;
      System.out.println("aaaa");
          RequestContext.getCurrentInstance().push("counter", count);
      }
  }
  </code></pre>
  
  <p>test.xhtml:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:p="http://primefaces.org/ui"&gt;
      &lt;h:head&gt;
      &lt;/h:head&gt;   
      &lt;h:body&gt;    
  
          &lt;p:spinner /&gt;   
  
          &lt;h:form&gt;
      &lt;h:outputText value="#{globalCounter.count}" styleClass="ui-widget display" /&gt;
  
      &lt;br /&gt;
  
      &lt;p:commandButton value="Click" actionListener="#{globalCounter.increment}" /&gt;
  &lt;/h:form&gt;
  
  &lt;p:push onmessage="handleMessage" channel="counter" /&gt;
  
  &lt;script type="text/javascript"&gt;
      function handleMessage(evt, data) {
              $('.display').html(data);
      }
  &lt;/script&gt;
  
  
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here's the code for the case when it returns null (all files are in the same project):</p>
  
  <p>ChatController.java:</p>
  
  <pre><code>    package org.primefaces.examples.view;
  
      import java.io.Serializable;
      import java.util.HashSet;
      import java.util.Set;
  
      import javax.faces.application.FacesMessage;
      import javax.faces.bean.ApplicationScoped;
      import javax.faces.bean.ManagedBean;
      import javax.faces.context.FacesContext;
      import javax.faces.event.ActionEvent;
  
      import org.primefaces.context.RequestContext;
  
  
      public class ChatController implements Serializable {
  
          private final static String CHANNEL = "chat";
  
          private String message;
  
          private String username;
  
          private boolean loggedIn;
  
          private Set&lt;String&gt; users = new HashSet&lt;String&gt;();
  
          public String getMessage() {
              return message;
          }
          public void setMessage(String message) {
              this.message = message;
          }
  
          public String getUsername() {
              return username;
          }
          public void setUsername(String username) {
              this.username = username;
          }
  
          public boolean isLoggedIn() {
              return loggedIn;
          }
          public void setLoggedIn(boolean loggedIn) {
              this.loggedIn = loggedIn;
          }
  
          public void send() {
              RequestContext.getCurrentInstance().push(CHANNEL, username + ": "+  message);
  
              message = null;
          }
  
          public void login() {
  
              RequestContext requestContext = RequestContext.getCurrentInstance();
  
              if(users.contains(username)) {
                  loggedIn = false;
                  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username taken", "Try with another username."));
  
                  requestContext.addPartialUpdateTarget("growl");
              }
              else {
                  users.add(username);
                  loggedIn = true;
  
                  requestContext.push(CHANNEL, username + " joined the channel.");
              }
          }
  
          public void disconnect() {
              RequestContext.getCurrentInstance().push(CHANNEL, username + " has left the channel.");
              loggedIn = false;
              username = null;
          }
      }
  </code></pre>
  
  <p>chat.xhtml</p>
  
  <pre><code>        &lt;html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:p="http://primefaces.org/ui"&gt;
  
          &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt;
          &lt;title&gt;Insert title here&lt;/title&gt;
          &lt;/head&gt;
          &lt;body&gt;
          &lt;f:view&gt;
          &lt;p:growl id="growl" showDetail="true" /&gt;
  
          &lt;h:form&gt;
  
              &lt;p:fieldset id="container" legend="PrimeChat"&gt;
  
                  &lt;h:panelGroup rendered="#{chatController.loggedIn}" &gt;
                      &lt;p:outputPanel layout="block" style="width:600px;height:200px;overflow:auto" 
                                 styleClass="chatroom" /&gt;
                      &lt;p:spinner /&gt;
                      &lt;p:separator /&gt;
  
                      &lt;p:inputText value="#{chatController.message}" styleClass="messageInput" /&gt;
                      &lt;p:spacer width="5" /&gt;
                      &lt;p:commandButton value="Send" actionListener="#{chatController.send}" global="false" oncomplete="$('.messageInput').val('').focus()"/&gt;
                      &lt;p:spacer width="5" /&gt;
                      &lt;p:commandButton value="Disconnect" actionListener="#{chatController.disconnect}" global="false" 
                                          oncomplete="chatAgent.close()" update="container" /&gt;
                  &lt;/h:panelGroup&gt;
  
                  &lt;h:panelGroup rendered="#{not chatController.loggedIn}" &gt;
                      Username: &lt;p:inputText value="#{chatController.username}" /&gt;
  
                      &lt;p:spacer width="5" /&gt;
                      &lt;p:commandButton value="Login" actionListener="#{chatController.login}" update="container" 
                                       icon="ui-icon-person"/&gt;
                  &lt;/h:panelGroup&gt;
  
              &lt;/p:fieldset&gt;
  
          &lt;/h:form&gt;
  
          &lt;p:push onmessage="handleMessage" channel="chat" widgetVar="chatAgent" /&gt;
  
          &lt;script type="text/javascript"&gt;
              function handleMessage(evt, data) {
                  var chatContent = $('.chatContent');
                  chatContent.append(data + '&lt;br /&gt;');
  
                  //keep scroll
                  chatContent.scrollTop(chatContent.height());
              }
          &lt;/script&gt;
          &lt;script type="text/javascript"&gt;
          function handleComplete(xhr, status, args) {
              if(args.validationFailed) {
                  PrimeFaces.debug("Validation Failed");
              } 
              else {
                  PrimeFaces.debug("Save:" + args.saved);
                  PrimeFaces.debug("FirstName: " + args.user.firstname + ", Lastname: " + args.user.lastname); 
              }
          }
          &lt;/script&gt;
  
          &lt;/f:view&gt;
          &lt;/body&gt;
          &lt;/html&gt;
  </code></pre>
  
  <p>faces-config.xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xi="http://www.w3.org/2001/XInclude"
       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-facesconfig_2_0.xsd"&gt;
       &lt;managed-bean eager="true"&gt;
        &lt;managed-bean-name&gt;chatController&lt;/managed-bean-name&gt;
        &lt;managed-bean-class&gt;org.primefaces.examples.view.ChatController&lt;/managed-bean-class&gt;
        &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;
       &lt;/managed-bean&gt;
       &lt;managed-bean&gt;
        &lt;managed-bean-name&gt;globalCounter&lt;/managed-bean-name&gt;
        &lt;managed-bean-class&gt;org.primefaces.examples.view.GlobalCounterBean&lt;/managed-bean-class&gt;
        &lt;managed-bean-scope&gt;application&lt;/managed-bean-scope&gt;
       &lt;/managed-bean&gt;
       &lt;!--
          No ManagedBean declarations here as we are using @ManagedBean Annotations.
          --&gt;
      &lt;/faces-config&gt;
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;web-app version="2.5"
              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_2_5.xsd" &gt;
  
              &lt;welcome-file-list&gt;
                  &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
              &lt;/welcome-file-list&gt;
  
              &lt;servlet&gt;
                  &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
                  &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
                  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
              &lt;/servlet&gt;
  
              &lt;servlet-mapping&gt;
                  &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
                  &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
              &lt;/servlet-mapping&gt;
  
          &lt;/web-app&gt;
  </code></pre>
  
  <p>And here's the exception thrown in tomcat console:</p>
  
  <pre><code>                        2012-05-27 19:16:25 com.sun.faces.context.ExceptionHandlerImpl log
              SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt2:j_idt16, Message=java.lang.NullPointerException
              2012-05-27 19:16:25 com.sun.faces.context.ExceptionHandlerImpl log
              SEVERE: java.lang.NullPointerException
              javax.faces.event.AbortProcessingException: java.lang.NullPointerException
                  at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:178)
                  at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84)
                  at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
                  at javax.faces.component.UICommand.broadcast(UICommand.java:296)
                  at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
                  at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
                  at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
                  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
                  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
                  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
                  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
                  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
                  at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
                  at java.lang.Thread.run(Thread.java:680)
              Caused by: java.lang.NullPointerException
                  at org.primefaces.examples.view.ChatController.login(ChatController.java:69)
                  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  at java.lang.reflect.Method.invoke(Method.java:597)
                  at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
                  at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                  at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
                  ... 21 more                    
  </code></pre>