title: What might be the reason for JSF/MyFaces to lose its FacesContext?
body: <p>I have a webapplication based on JSF(2.1.17) myfaces /PrimeFaces + Omnifaces on WAS 8.0.0.9</p>
  
  <p>Most of the time everything works ok, but occasionally I get the following exception:</p>
  
  <pre><code>java.lang.NullPointerException
          at org.apache.myfaces.shared.context.flash.FlashImpl.isKeepMessages(FlashImpl.java:388)
          at org.apache.myfaces.shared.context.flash.FlashImpl._saveMessages(FlashImpl.java:668)
          at org.apache.myfaces.shared.context.flash.FlashImpl.doPostPhaseActions(FlashImpl.java:269)
          at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:254)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)
          at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1230)
          at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
          at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
          at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
          at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)
          at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)
          at org.omnifaces.filter.FacesExceptionFilter.doFilter(FacesExceptionFilter.java:56)
          at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:77)
          at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
          at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
  </code></pre>
  
  <p>// ... more filters ...</p>
  
  <p>When looking at <code>FlashImpl</code> line388 if find:</p>
  
  <pre><code>@Override
  public boolean isKeepMessages()
  {
      FacesContext facesContext = FacesContext.getCurrentInstance();
      ExternalContext externalContext = facesContext.getExternalContext(); //&lt;-- line 388
      Map&lt;String, Object&gt; requestMap = externalContext.getRequestMap();
      Boolean keepMessages = (Boolean) requestMap.get(FLASH_KEEP_MESSAGES);
  
      return (keepMessages == null ? Boolean.FALSE : keepMessages);
  }
  </code></pre>
  
  <p>So it looks like there is no <code>FacesContext</code>. Although (again from looking at the code) it should get created in <code>FacesServlet.service</code> further up the call stack.</p>
  
  <p>One of the filters we have registered logs time used in the different phases of the JSF lifecycle, and it complains that the phase RENDER_RESPONSE is started, although it is already running.</p>
  
  <p>Logging is also suggesting that in the current request the requested page could not be found and therefor the apropriate error page is requested.</p>
  
  <p>All this might happen during an Ajax request but I am not sure.</p>
  
  <p>Any ideas what might be causing this problem?</p>
  
  <p>I found <a href="https://issues.apache.org/jira/browse/MYFACES-3673" rel="nofollow noreferrer">this bugreport</a> but couldn't find a reference to <code>getRequestDispatcher</code> so it doesn't seem to apply.</p>
  
  <p>I also found this question: <a href="https://stackoverflow.com/questions/20465195/null-pointer-exception-in-flashimpl-java-in-ibm-jsf">Null pointer exception in FlashImpl.java in IBM JSF</a> which actually might be a duplicate but since I have more (possibly relevant) information which might or might not apply to the other question I thought it better to create a new one.</p>