8 Replies Latest reply on Feb 10, 2011 3:58 AM by mavihemi mavihemi

    NullPointerException sometimes when FacesBean.getApplication()

    mavihemi mavihemi Newbie

      Hi,

       

        I'm developping a JsfUnit test that runs sometimes. When it fails is setting a session bean attribute in the setup() method.

      First of all I do:

       

      JSFSession jsfSession = new JSFSession("/faces/BuscarPersona.jsp");

      client = jsfSession.getJSFClientSession();

      server = jsfSession.getJSFServerSession();

      instance = new EditarVacaciones();

      instance.getSessionBean1().setAñoSel("2009");

       

       

      See the stacktrace:

       

      java.lang.NullPointerException

      at com.sun.rave.web.ui.appbase.FacesBean.getApplication(FacesBean.java:95)

      at com.sun.rave.web.ui.appbase.FacesBean.getBean(FacesBean.java:231)

      at permisosylicencias.AniadirVacaciones.getApplicationBean1(AniadirVacaciones.java:199)

      at permisosylicencias.AniadirVacacionesTest.setUp(AniadirVacacionesTest.java:48)

      at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)

      at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)

      at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)

      at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)

      at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)

      at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)

      at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)

      at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)

      at org.jboss.jsfunit.framework.JSFUnitServletRedirector.doPost(JSFUnitServletRedirector.java:46)

      at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72)

      at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:224)

      at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)

      at org.jboss.jsfunit.framework.JSFUnitServletRedirector.doGet(JSFUnitServletRedirector.java:52)

      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

      at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

      at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)

      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)

      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)

      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)

      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)

      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)

      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)

      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)

      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)

      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)

      at java.lang.Thread.run(Thread.java:619)

       

       

      Any idea why sometimes the error arise and not other times.

       

      Thanks.

        • 1. NullPointerException sometimes when FacesBean.getApplication()
          Stan Silvert Master

          It's hard to know exactly what is happening.  First, your stack trace doesn't match your code.  Your code shows getSessionBean1() and the stack trace has getApplicationBean1().

           

          I don't know what rave is doing.  I suspect that somehow it is doing something in a servlet filter that interferes with JSFUnit.  It might be invalidating the session or disposing of the FacesContext.  But those are just guesses at this point. 

           

          Stan

          • 2. NullPointerException sometimes when FacesBean.getApplication()
            mavihemi mavihemi Newbie

            You are right, the stack trace is wrong. The right one is:

             

            java.lang.NullPointerException

            at com.sun.rave.web.ui.appbase.FacesBean.getApplication(FacesBean.java:95)

            at com.sun.rave.web.ui.appbase.FacesBean.getBean(FacesBean.java:231)

            at permisosylicencias.EditarVacaciones.getSessionBean1(EditarVacaciones.java:231)

            at permisosylicencias.EditarVacacionesTest.testEditButton_action_CASO1(EditarVacacionesTest.java:118)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

            at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)

            at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)

            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)

            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)

            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)

            at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)

            at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)

            at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)

            at org.jboss.jsfunit.framework.JSFUnitServletRedirector.doPost(JSFUnitServletRedirector.java:46)

            at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72)

            at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:224)

            at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)

            at org.jboss.jsfunit.framework.JSFUnitServletRedirector.doGet(JSFUnitServletRedirector.java:52)

            at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

            at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)

            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

            at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)

            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

            at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)

            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)

            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)

            at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)

            at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)

            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

            at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)

            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)

            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)

            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)

            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)

            at java.lang.Thread.run(Thread.java:619)

             

            I am doing five test cases, sometimes the first four are done and the five gets this error. Another times the first one gets this error and the following goes well.

             

            I tried with a sleep() before "getSessionBean1" but it doesn't solve my problem.

             

            Thanks a lot.

            mavihemi.

            • 3. NullPointerException sometimes when FacesBean.getApplication()
              Stan Silvert Master

              Is Rave using a servlet filter?  Please post your web.xml.

               

              Stan

              • 4. NullPointerException sometimes when FacesBean.getApplication()
                mavihemi mavihemi Newbie

                This is my 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.STATE_SAVING_METHOD</param-name>

                        <param-value>client</param-value>

                    </context-param>

                    <context-param>

                        <param-name>com.sun.faces.validateXml</param-name>

                        <param-value>true</param-value>

                    </context-param>

                    <context-param>

                        <param-name>com.sun.faces.verifyObjects</param-name>

                        <param-value>false</param-value>

                    </context-param>

                    <filter>

                        <filter-name>UploadFilter</filter-name>

                        <filter-class>com.sun.webui.jsf.util.UploadFilter</filter-class>

                        <init-param>

                            <description>The maximum allowed upload size in bytes.  If this is set to a negative value, there is no maximum.  The default value is 1000000.</description>

                            <param-name>maxSize</param-name>

                            <param-value>1000000</param-value>

                        </init-param>

                        <init-param>

                            <description>The size (in bytes) of an uploaded file which, if it is exceeded, will cause the file to be written directly to disk instead of stored in memory.  Files smaller than or equal to this size will be stored in memory.  The default value is 4096.</description>

                            <param-name>sizeThreshold</param-name>

                            <param-value>4096</param-value>

                        </init-param>

                    </filter>

                    <filter>

                        <filter-name>JSFUnitFilter</filter-name>

                        <filter-class>org.jboss.jsfunit.framework.JSFUnitFilter</filter-class>

                    </filter>

                    <filter-mapping>

                        <filter-name>UploadFilter</filter-name>

                        <servlet-name>Faces Servlet</servlet-name>

                    </filter-mapping>

                    <filter-mapping>

                        <filter-name>JSFUnitFilter</filter-name>

                        <servlet-name>ServletTestRunner</servlet-name>

                    </filter-mapping>

                    <filter-mapping>

                        <filter-name>JSFUnitFilter</filter-name>

                        <servlet-name>ServletRedirector</servlet-name>

                    </filter-mapping>

                    <servlet>

                        <servlet-name>Faces Servlet</servlet-name>

                        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

                        <init-param>

                            <param-name>javax.faces.LIFECYCLE_ID</param-name>

                            <param-value>com.sun.faces.lifecycle.PARTIAL</param-value>

                        </init-param>

                        <load-on-startup>1</load-on-startup>

                    </servlet>

                    <servlet>

                        <servlet-name>ExceptionHandlerServlet</servlet-name>

                        <servlet-class>com.sun.errorhandler.ExceptionHandler</servlet-class>

                        <init-param>

                            <param-name>errorHost</param-name>

                            <param-value>localhost</param-value>

                        </init-param>

                        <init-param>

                            <param-name>errorPort</param-name>

                            <param-value>24444</param-value>

                        </init-param>

                    </servlet>

                    <servlet>

                        <servlet-name>ThemeServlet</servlet-name>

                        <servlet-class>com.sun.webui.theme.ThemeServlet</servlet-class>

                    </servlet>

                    <servlet>

                        <servlet-name>errorHandler</servlet-name>

                        <servlet-class>error.errorHandler</servlet-class>

                    </servlet>

                    <servlet>

                        <servlet-name>ServletRedirector</servlet-name>

                        <servlet-class>org.jboss.jsfunit.framework.JSFUnitServletRedirector</servlet-class>

                    </servlet>

                    <servlet>

                        <servlet-name>ServletTestRunner</servlet-name>

                        <servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class>

                    </servlet>

                    <servlet-mapping>

                        <servlet-name>Faces Servlet</servlet-name>

                        <url-pattern>/faces/*</url-pattern>

                    </servlet-mapping>

                    <servlet-mapping>

                        <servlet-name>ExceptionHandlerServlet</servlet-name>

                        <url-pattern>/error/ExceptionHandler</url-pattern>

                    </servlet-mapping>

                    <servlet-mapping>

                        <servlet-name>ThemeServlet</servlet-name>

                        <url-pattern>/theme/*</url-pattern>

                    </servlet-mapping>

                    <servlet-mapping>

                        <servlet-name>errorHandler</servlet-name>

                        <url-pattern>/error/errorHandler</url-pattern>

                    </servlet-mapping>

                    <servlet-mapping>

                        <servlet-name>ServletRedirector</servlet-name>

                        <url-pattern>/ServletRedirector</url-pattern>

                    </servlet-mapping>

                    <servlet-mapping>

                        <servlet-name>ServletTestRunner</servlet-name>

                        <url-pattern>/ServletTestRunner</url-pattern>

                    </servlet-mapping>

                    <session-config>

                        <session-timeout>

                            30

                        </session-timeout>

                    </session-config>

                    <welcome-file-list>

                        <welcome-file>faces/BuscarPersona.jsp</welcome-file>

                    </welcome-file-list>

                    <error-page>

                        <exception-type>javax.servlet.ServletException</exception-type>

                        <location>/error/errorHandler</location>

                    </error-page>

                    <error-page>

                        <exception-type>java.io.IOException</exception-type>

                        <location>/error/errorHandler</location>

                    </error-page>

                    <error-page>

                        <exception-type>javax.faces.FacesException</exception-type>

                        <location>/error/errorHandler</location>

                    </error-page>

                    <error-page>

                        <exception-type>com.sun.rave.web.ui.appbase.ApplicationException</exception-type>

                        <location>/error/errorHandler</location>

                    </error-page>

                    <jsp-config>

                        <jsp-property-group>

                            <url-pattern>*.jspf</url-pattern>

                            <is-xml>true</is-xml>

                        </jsp-property-group>

                        </jsp-config>

                    </web-app>

                • 5. Re: NullPointerException sometimes when FacesBean.getApplication()
                  Stan Silvert Master

                  I'm not exactly sure what is happening.  I've looked at the source for FacesBean and the getApplication() method should work.  However, I suggest that you change your test code.  When you call new EditarVacaciones() you are creating a bean outside of a JSF request.  The javadoc for FacesBean says:

                  NOTE - These integration methods will operate successfully only within the lifetime of a JavaServer Faces request.

                   

                  So there are no guarantees.

                   

                  In JSFUnit, the typical usage pattern is to create JSF requests and then assert the client and server side state.  Creation of new state inside your test is discouraged.

                   

                  Stan

                  • 6. Re: NullPointerException sometimes when FacesBean.getApplication()
                    mavihemi mavihemi Newbie

                    Thanks a lot !!

                     

                    so, How do you suggest to create this objects and associate them to the JSF request?

                     

                    mavihemi.

                    • 7. NullPointerException sometimes when FacesBean.getApplication()
                      Stan Silvert Master

                      You let the object be created as part of your applicaiton.  Put it in an action or a phase listener.

                       

                      But also think about if this is how you want to test things.  If you are artificially creating objects that wouldn't be created by a normal user then you might not be doing valid tests.

                       

                      Stan

                      1 of 1 people found this helpful
                      • 8. NullPointerException sometimes when FacesBean.getApplication()
                        mavihemi mavihemi Newbie

                        Thanks, I understand.

                         

                        Maybe I'm not be very polite using these classes.

                         

                        mavihemi.