title: Inject CDI null
body: <p>I set up a project to Inject bean
  But when you try to start getting a null pointer error
  Can you tell that I did not ?</p>
  
  <p><strong>public_html\WEB-INF\faces-config.xml</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;faces-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                version="2.2"
                xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"&gt;
      &lt;application&gt;
          &lt;locale-config&gt;
              &lt;default-locale&gt;ru&lt;/default-locale&gt;
          &lt;/locale-config&gt;
  
          &lt;resource-bundle&gt;
              &lt;base-name&gt;by.softclub.abank.admin.ui.resources.Labels&lt;/base-name&gt;
              &lt;var&gt;lbl&lt;/var&gt;
          &lt;/resource-bundle&gt;
  
          &lt;resource-bundle&gt;
              &lt;base-name&gt;by.softclub.abank.admin.ui.resources.Regexp&lt;/base-name&gt;
              &lt;var&gt;regexp&lt;/var&gt;
          &lt;/resource-bundle&gt;
  
          &lt;action-listener&gt;org.primefaces.application.DialogActionListener&lt;/action-listener&gt;
          &lt;navigation-handler&gt;org.primefaces.application.DialogNavigationHandler&lt;/navigation-handler&gt;
  
          &lt;!--   &lt;view-handler&gt;org.primefaces.application.DialogViewHandler&lt;/view-handler&gt;  --&gt;
          &lt;el-resolver&gt;
              org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
          &lt;/el-resolver&gt;
      &lt;/application&gt;
  
      &lt;factory&gt;
          &lt;exception-handler-factory&gt;
              org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
          &lt;/exception-handler-factory&gt;
      &lt;/factory&gt;
  
      &lt;lifecycle&gt;
          &lt;phase-listener&gt;by.softclub.abank.admin.ui.faces.UniqueSessionPhaseListener&lt;/phase-listener&gt;
      &lt;/lifecycle&gt;
  
  &lt;/faces-config&gt;
  </code></pre>
  
  <p><strong>public_html\WEB-INF\beans.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
         version="1.2" bean-discovery-mode="annotated"&gt;
  
      &lt;!-- some content --&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>UniqueSessionPhaseListener</strong> </p>
  
  <pre><code>public class UniqueSessionPhaseListener implements PhaseListener {
  
      @Inject
      ApplicationBean applicationBean;
  
      public PhaseId getPhaseId() {
          return PhaseId.RENDER_RESPONSE;
      }
  
      public void afterPhase(PhaseEvent phaseEvent) {
      }
  
      public void beforePhase(PhaseEvent phaseEvent) {
          if (applicationBean.sessionIntercepted(FacesUtils.getSession())) {
              SessionBean sessionBean = (SessionBean) FacesUtils.getSession().getAttribute("sessionBean");
              if (sessionBean.isAuthenticated()) {
                  sessionBean.logoutAction();
              }
          }
      }
  
  }
  </code></pre>
  
  <p><strong>ApplicationBean</strong> </p>
  
  <pre><code>@ApplicationScoped
  @Named("applicationBean")
  public class ApplicationBean implements Serializable {
      public ApplicationBean() {
      }
  
  }
  </code></pre>
  
  <p><strong>Error log</strong></p>
  
  <pre><code>09:35:58,189 ERROR [stderr] (default task-35) java.lang.NullPointerException
  09:35:58,189 ERROR [stderr] (default task-35)   at by.softclub.abank.admin.ui.faces.UniqueSessionPhaseListener.beforePhase(UniqueSessionPhaseListener.java:26)
  09:35:58,189 ERROR [stderr] (default task-35)   at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:228)
  09:35:58,190 ERROR [stderr] (default task-35)   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:99)
  09:35:58,190 ERROR [stderr] (default task-35)   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
  09:35:58,190 ERROR [stderr] (default task-35)   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)
  09:35:58,190 ERROR [stderr] (default task-35)   at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
  09:35:58,190 ERROR [stderr] (default task-35)   at com.github.zhanhb.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:55)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
  09:35:58,190 ERROR [stderr] (default task-35)   at by.softclub.abank.admin.ui.faces.UrlFilter.doFilter(UrlFilter.java:118)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  09:35:58,190 ERROR [stderr] (default task-35)   at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
  09:35:58,190 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  09:35:58,193 ERROR [stderr] (default task-35)   at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
  09:35:58,193 ERROR [stderr] (default task-35)   at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
  09:35:58,193 ERROR [stderr] (default task-35)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  09:35:58,193 ERROR [stderr] (default task-35)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  09:35:58,193 ERROR [stderr] (default task-35)   at java.lang.Thread.run(Thread.java:745)
  09:35:58,193 SEVERE [org.primefaces.application.exceptionhandler.PrimeExceptionHandler] (default task-35) null: java.lang.NullPointerException
      at by.softclub.abank.admin.ui.faces.UniqueSessionPhaseListener.beforePhase(UniqueSessionPhaseListener.java:26) [classes:]
      at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:228) [jsf-impl-2.2.12-jbossorg-2.jar:2.2.12-jbossorg-2]
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:99) [jsf-impl-2.2.12-jbossorg-2.jar:2.2.12-jbossorg-2]
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) [jsf-impl-2.2.12-jbossorg-2.jar:2.2.12-jbossorg-2]
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) [jboss-jsf-api_2.2_spec-2.2.12.jar:2.2.12]
      at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98) [primefaces-5.0.jar:5.0]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at com.github.zhanhb.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:55) [character-encoding-filter-2.1.6.4.jar:]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at by.softclub.abank.admin.ui.faces.UrlFilter.doFilter(UrlFilter.java:118) [classes:]
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172) [undertow-servlet-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774) [undertow-core-1.2.9.Final.jar:1.2.9.Final]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_102]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_102]
      at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_102]
  </code></pre>
  
  <p>I try</p>
  
  <p><a href="https://stackoverflow.com/questions/19930241/how-to-inject-in-a-phaselistener">How to @Inject in a PhaseListener</a></p>
  
  <p>But also null pointer</p>
  
  <pre><code>public static ApplicationBean getApplicationBean() {
      FacesContext context = FacesContext.getCurrentInstance();
      return context.getApplication()
              .evaluateExpressionGet(context, "#{applicationBean}", ApplicationBean.class);
  }
  </code></pre>
  
  <p>Whee ApplicationBean was annotated @ManagedBean all work fine</p>
  
  <p>Change PhaseListener</p>
  
  <pre><code>import javax.el.ELContext;
  import javax.enterprise.context.ApplicationScoped;
  import javax.faces.application.Application;
  import javax.faces.context.FacesContext;
  import javax.faces.event.PhaseEvent;
  import javax.faces.event.PhaseId;
  import javax.faces.event.PhaseListener;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  @ApplicationScoped
  public class UniqueSessionPhaseListener implements PhaseListener {
  
      public PhaseId getPhaseId() {
          return PhaseId.RENDER_RESPONSE;
      }
  
      public void afterPhase(PhaseEvent phaseEvent) {
      }
  
      public void beforePhase(PhaseEvent phaseEvent) {
          FacesContext context = FacesContext.getCurrentInstance();
          ELContext elContext = context.getELContext();
  
          Object object = context.getApplication().evaluateExpressionGet(context, "#{applicationBean}", Object.class);
  
          Application application = context.getApplication();
          ApplicationBean applicationBean0 = application.evaluateExpressionGet(context, "#{applicationBean}", ApplicationBean.class);
          ApplicationBean applicationBean00 = (ApplicationBean) application.getExpressionFactory().createValueExpression(elContext, "#{applicationBean}", ApplicationBean.class).getValue(elContext);
          ApplicationBean applicationBean000 = (ApplicationBean) elContext.getELResolver().getValue(elContext, null, "applicationBean");
  
          ApplicationBean applicationBean = (ApplicationBean) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "#{applicationBean}");
          ApplicationBean applicationBean1 = context.getApplication().evaluateExpressionGet(context, "#{applicationBean}", ApplicationBean.class);
  
          InitialContext initialContext = null;
          try {
              initialContext = new InitialContext();
              ApplicationBean beanManager = (ApplicationBean) initialContext.lookup("java:comp/applicationBean");
          } catch (NamingException e) {
              e.printStackTrace();
          }
  
  
          ApplicationBean bean = FacesUtils.getApplicationBean();
          if (bean.sessionIntercepted(FacesUtils.getSession())) {
              SessionBean sessionBean = (SessionBean) FacesUtils.getSession().getAttribute("sessionBean");
              if (sessionBean.isAuthenticated()) {
                  sessionBean.logoutAction();
              }
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Updated 21.09.2016</strong></p>
  
  <p>Add questions:</p>
  
  <p><em>1) How can I find is on the right path lies beans.xml (faces-config is in the WEB-INF), so I think unlikely but still there is the idea that with the beans.xml that something is wrong.
  2) Server Settings, his library (I WildFly 9) can not be to influence the CDI application?</em></p>