0

I'm trying to implement logic in JSF that accepts parameters and then updates some output on the xhtml page. The following is all borrowed from the primefaces pushdata showcase

XHTML:

<f:view>
                <h:form id="form">
                    <f:metadata>
                        <f:viewParam name="data" value="#{viewParamPush.data}" />
                        <f:event type="preRenderView" listener="#{viewParamPush.prerender}" />
                    </f:metadata>

                    <h:outputText id="out" value="#{viewParamPush.data}" style="font-size:16px" />

                    <p:socket onMessage="handleMessage" channel="/QR" />

                    <script type="text/javascript">
                        function handleMessage(data) {
                            $('#out').text(data);
                        }
                    </script>
                </h:form>
            </f:view>

ViewParamPush:

    ManagedBean
    @RequestScoped
    public class ViewParamPush implements Serializable{

        private String data;

        public String getData() {
            return data;
        }

        public void setData(String data) {
            util.sysprint("setData " + data, true);
            this.data = data;
        }

        public void prerender() {
            util.sysprint("prerender", true);
            EventBus eventBus = EventBusFactory.getDefault().eventBus();
            util.sysprint("prerender2", true);
            even

tBus.publish("/QR", data);
        util.sysprint("prerender3", true);
    }
}

ViewParamResource:

@PushEndpoint("/QR")
public class ViewParamResource {

    @OnMessage(encoders = {JSONEncoder.class})
    public String onMessage(String data) {
         util.sysprint("ViewParamResource onMessage", true);
        return StringEscapeUtils.escapeHtml4(data);
    }
}

I get the following exception when eventBus.publish("/QR", data); is called. I understand what a null pointer is however I don't understand why publish("/QR", data); in this case is causing one. Is there something wrong with my syntax?

FATAL:   JSF1073: javax.el.ELException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=/QR.xhtml @21,95 listener="#{viewParamPush.prerender}": java.lang.NullPointerException
FATAL:   /QR.xhtml @21,95 listener="#{viewParamPush.prerender}": java.lang.NullPointerException
javax.el.ELException: /QR.xhtml @21,95 listener="#{viewParamPush.prerender}": java.lang.NullPointerException 

I added *.xhtml to the url patterns in order to be able to call the script QR.xhtml?data=some data.. I assume that required in order to pass parameters to the script although I wonder if that affects the publish method

<servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
        **<url-pattern>*.xhtml</url-pattern>**
    </servlet-mapping>

Dependencies:

 <dependencies>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.3</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>appserv-rt</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/org.atmosphere/atmosphere-runtime -->
        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.marklogic</groupId>
            <artifactId>java-client-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>  
            <groupId>org.primefaces.themes</groupId>  
            <artifactId>cupertino</artifactId>  
            <version>1.0.10</version>  
        </dependency>
        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>primefaces-extensions</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.helger</groupId>
            <artifactId>ph-schematron</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
            <exclusions>
                <exclusion>
                    <groupId>bouncycastle</groupId>
                    <artifactId>bcprov-jdk14</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>bouncycastle</groupId>
                    <artifactId>bcmail-jdk14</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bctsp-jdk14</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j-light</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>net.glxn</groupId>
            <artifactId>qrgen</artifactId>  <!-- QR code support -->
            <version>1.4</version>
        </dependency>
    </dependencies>
ElitCenk
  • 302
  • 3
  • 10
conteh
  • 1,544
  • 1
  • 17
  • 39
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Kukeltje Jun 01 '16 at 12:57
  • I understand what a null pointer is however I don't understand why publish("/QR", data); in this case is causing one. Is there something wrong with my syntax? – conteh Jun 01 '16 at 13:43
  • There is an NPE in your code (`#{viewParamPush.prerender}`)... check that, debug, etc.... that is what the 'duplicate' is all about. Then maybe create a new question asking WHY something is null... – Kukeltje Jun 01 '16 at 14:08
  • That much is very very clear. The NPE occurs when eventBus.publish("/QR", data) is called as I mentioned which is within the prerender method. As far as I know that is referring to the channel="/QR" in the p:socket. The problem is I don't understand why it's generating a NPE in this instance. – conteh Jun 01 '16 at 16:04
  • That much is clear to you, at least you think it is... A stacktrace tells you **where** it happens. We are not clairvoyant. Sharing the stacktrace AND indicating which line in the bean a linenumber is, is of utmost importance. Add a try catch and find out. That is what the duplicat is about. This post/question is therefor still lacking info and therefor a duplicate..Good luck – Kukeltje Jun 01 '16 at 18:01
  • If you'd actually investigated the npe, your question would have been identical to http://stackoverflow.com/questions/31985116/nullpointerexception-when-using-primepush. See the stacktrace there? And hence the better more explicit question. – Kukeltje Jun 01 '16 at 18:07
  • That's a bit condescending to suggest that I didn't investigate. More accurate would be that I'm new to JSF and was searching with the wrong parameters. – conteh Jun 01 '16 at 23:02

1 Answers1

4

I've previously encountered this error. The reason for this failure is due to the Atmosphere version.

Then use it as below :

pom.xml

     <dependency>
        <groupId>org.atmosphere</groupId>
        <artifactId>atmosphere-runtime</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.atmosphere</groupId>
        <artifactId>atmosphere-runtime-native</artifactId>
        <version>2.4.3</version>
    </dependency>

web.xml

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

This way you can try. It works this for me.

Ömer Faruk Kurt
  • 500
  • 3
  • 14
  • So this question is actuallyba duplicate of http://stackoverflow.com/questions/31985116/nullpointerexception-when-using-primepush. Please next time mark it as such. It keeps stackoverflow clean. Tnx – Kukeltje Jun 01 '16 at 18:09
  • While I see the similarity, that one did not fully answer my question. The configuration that Kurt put forward differs and solved my problem. I would disagree that having this question and solution causes any harm. it does certainly build upon that other question with a more up to date solution. Please though have someone else weigh in on it. Best Regards – conteh Jun 01 '16 at 23:04