13 Replies Latest reply: Feb 21, 2012 12:39 PM by buraq RSS

    CDI Push NullPointerException while Injecting pushEvent

    buraq Newbie

      Hi,

      I'm trying to get a4j:push working with CDI events on 4.1.0.Final.

      My environment;

       

      Netbeans 7.0.1

      Tomcat 7.0.14.0

      Mojarra 2.0.2

      RichFaces 4.1.0

      PrimeFaces 2.2.1

       

      web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <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">
          <context-param>
              <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
              <param-value>true</param-value>
          </context-param>
      
          <context-param>
              <param-name>org.richfaces.push.jms.disable</param-name>
              <param-value>true</param-value>
          </context-param>
      
          <filter>
              <filter-name>PushFilter</filter-name>
              <filter-class>org.richfaces.webapp.PushFilter</filter-class>
          </filter>
      
          <filter-mapping>
              <filter-name>PushFilter</filter-name>
              <servlet-name>Faces Servlet</servlet-name>
          </filter-mapping>
      
          <context-param>
              <param-name>com.sun.faces.expressionFactory</param-name>
              <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
          </context-param>
      
          <listener>
              <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
          </listener>
      
          <servlet>
              <servlet-name>Faces Servlet</servlet-name>
              <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
      
          <servlet-mapping>
              <servlet-name>Faces Servlet</servlet-name>
              <url-pattern>*.jsf</url-pattern>
              <url-pattern>*.xhtml</url-pattern>
              <url-pattern>*.go</url-pattern>
          </servlet-mapping>
      
          <session-config>
              <session-timeout>
                  30
              </session-timeout>
          </session-config>
      
          <welcome-file-list>
              <welcome-file>login.xhtml</welcome-file>
          </welcome-file-list>
      
          <error-page>
              <exception-type>javax.faces.application.ViewExpiredException</exception-type>
              <location>/login.xhtml</location>
          </error-page>
      </web-app>
      

       

       

      producer.xhtml

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                      xmlns:h="http://java.sun.com/jsf/html"
                      xmlns:ui="http://java.sun.com/jsf/facelets"
                      xmlns:f="http://java.sun.com/jsf/core"
                      xmlns:p="http://primefaces.prime.com.tr/ui"
                      xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                      template="menu.xhtml">
      
          <ui:define name="pageContent">
              <f:view contentType="text/html" >
      
                  <br/>
                  <h:form id="mf" prependId="false">
                      <p:panel>                    
                          <p:commandButton value="PUSH" actionListener="#{myManagedBean.push}"/>
                      </p:panel>
                  </h:form>
              </f:view>
          </ui:define>
      </ui:composition>
      

       

      consumer.xhtml

       

      <a4j:push address="#{myManagedBean.userIdentifier}@pushCdi"
                      ondataavailable="jQuery('&lt;li /&gt;').prependTo('#messages').text(event.rf.data)" /> 
       <ul id="messages" />
      

       

      MyManagedBean.java

       

      import javax.annotation.PostConstruct;
      import javax.faces.bean.ViewScoped;
      import javax.enterprise.event.Event;
      import javax.faces.bean.ManagedBean;
      import javax.faces.context.FacesContext;
      import javax.inject.Inject;
      import org.richfaces.cdi.push.Push;
      
      
      @ManagedBean
      @ViewScoped
      public class MyManagedBean extends BaseBean implements java.io.Serializable{
      
           private static final long serialVersionUID = 1L;
          public static final String PUSH_CDI_TOPIC = "pushCdi";
      
          private String userIdentifier;
          private String message;
      
          @Inject
          @Push(topic = PUSH_CDI_TOPIC, subtopic = "#{myManagedBean.userIdentifier}")
          Event<String> pushEvent;
      
          @PostConstruct
          public void initialize() {
              super.initBean();
              if (userIdentifier == null) {
                  userIdentifier = sessionUser.getId();
              }
          }
      
          public synchronized void push() throws Exception {
      
                  pushEvent.fire("PushTest");
          }
      
          public String getMessage() {
              return message;
          }
      
          public void setMessage(String message) {
              this.message = message;
          }
      
          public Event<String> getPushEvent() {
              return pushEvent;
          }
      
          public void setPushEvent(Event<String> pushEvent) {
              this.pushEvent = pushEvent;
          }
      
          public String getUserIdentifier() {
              return userIdentifier;
          }
      
          public void setUserIdentifier(String userIdentifier) {
              this.userIdentifier = userIdentifier;
          }
      
      }
      

       

      When I click the "PUSH" button in my producer.xhtml I'm getting  java.lang.NullPointerException at "pushEvent.fire("PushTest");"  line. Is there a problem when using @Inject ?

       

      Thanks,

      Buraq