Primefaces 6.0, Atmosphere 2.4.8, Push NPE

UI Components for JSF
Post Reply
djanthony93
Posts: 7
Joined: 18 Nov 2016, 00:29

19 Nov 2016, 01:17

I'm not able to get rid of this NPE. I have tried all solutions on this forum but nothing fixed my NPE. Please take a look, my application is very simple.

web.xml

Code: Select all

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app 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/web-app_3_1.xsd"
         version="3.1">

  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>home</param-value>
  </context-param>

  <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>*.xhtml</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>Push Servlet</servlet-name>
    <servlet-class>org.primefaces.push.PushServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
      <param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
      <param-value>org.atmosphere.cache.UUIDBroadcasterCache</param-value>
    </init-param>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>Push Servlet</servlet-name>
    <url-pattern>/primepush/*</url-pattern>
  </servlet-mapping>

</web-app>
Item.class

Code: Select all


public class Item {

    private String label;
    private String value;

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
Bean.class

Code: Select all

import org.primefaces.push.EventBus;
import org.primefaces.push.EventBusFactory;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.impl.JSONEncoder;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@ManagedBean
@ViewScoped
@PushEndpoint("/notify")
public class Bean implements Serializable{

    private List<Item> items;

    @PostConstruct
    public void init() {
        items = new ArrayList<Item>();
    }

    public void start() {
        ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
        timer.scheduleAtFixedRate(()->add(),0,3,TimeUnit.SECONDS);
    }

    public void add() {
        Item item = new Item();
        item.setLabel("label" + items.size());
        items.add(item);

        EventBus eventBus = EventBusFactory.getDefault().eventBus();
        eventBus.publish("/notify", item);
    }

    @OnMessage(encoders = {JSONEncoder.class})
    public Item onMessage(Item item){
        return item;
    }

    public void remove(Item item) {
        items.remove(item);
    }

    public List<Item> getItems() {
        return items;
    }
}
index.xhtml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui" xmlns:ui="http://java.sun.com/jsf/facelets"
      xml:lang="en" lang="en">
<h:head>
    <title>GG Well Trade</title>
</h:head>
<h:body>
    <h:form>
        <p:socket channel="/notify" onMessage="#{bean.add()}" update="content"/>
        <p:commandButton value="Start" actionListener="#{bean.start()}"/>
        <ui:repeat value="#{bean.items}" var="item" id="content">
            <p:outputLabel for="foo" value="#{item.label}" />
            <p:inputText id="foo" value="#{item.value}" />
            <p:commandButton value="Remove" actionListener="#{bean.remove(item)}" update="@form" />
            <br/>
        </ui:repeat>
        <!--<p:commandButton value="Add" action="#{bean.add}" update="@form" />-->
    </h:form>
</h:body>
</html>
The NPE is caused by Bean:44, as the localhost:8080 says:

Code: Select all

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
	Bean.add(Bean.java:44)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	javax.el.BeanELResolver.invoke(BeanELResolver.java:158)
	javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
	org.apache.el.parser.AstValue.getValue(AstValue.java:159)
	org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
	com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
	javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
	org.primefaces.component.socket.Socket.getOnMessage(Socket.java:125)
	org.primefaces.component.socket.SocketRenderer.encodeEnd(SocketRenderer.java:55)
	javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
	javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
	javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
	javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
	javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
	javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
	javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
	com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
	com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
	com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
	com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
	com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.8 logs.

Apache Tomcat/8.5.8
The line is the following:

Code: Select all

eventBus.publish("/notify", item);
I don't know what's wrong, please help me. Thanks.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 34 guests