webtier@glassfish.java.net

JSF/JSF2 Seemingly random NullPointerExceptions in request.get/setAttribute() using Filters

From: Lincoln Baxter, III <lincolnbaxter_at_gmail.com>
Date: Mon, 06 Apr 2009 18:04:17 -0400

Hi Gurus,

Got a question...

I'm using PrettyFaces for JSF (trying to update it for JSF2,) and I
started getting some NullPointerExceptions that I don't feel like were
occurring before.

Here are the steps that occur:

1. PrettyFaces intercepts all requests to the WebApp, it checks against
a configuration file to determine whether or not that request should be
forwarded to JSF.

2. PrettyFaces then constructs a FacesContext in order to do some work
with FacesMessages, :

    public FacesContext getFacesContext(final ServletRequest request,
final ServletResponse response)
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null)
        {
            return facesContext;
        }

        FacesContextFactory contextFactory = (FacesContextFactory)
FactoryFinder
                .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
        LifecycleFactory lifecycleFactory = (LifecycleFactory)
FactoryFinder
                .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
        Lifecycle lifecycle =
lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

        ServletContext servletContext = ((HttpServletRequest)
request).getSession().getServletContext();
        facesContext = contextFactory.getFacesContext(servletContext,
request, response, lifecycle);

InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);

        return facesContext;
    }

3. PrettyFaces constructs a PrettyContext and PrettyConfig object, and
stores them using request.setAttribute()

4. PrettyFaces forwards to JSF (or not, if not applicable)

It seems like these attributes are sometimes lost during the forward to
the JSF servlet. This must have something to do with threading and
forwards, but I can't seem to narrow it down. Any help would be greatly
appreciated.

Sometimes the Exceptions occur even before the forward to JSF.


        Caused by: java.lang.NullPointerException
        at
        org.apache.catalina.connector.Request.setAttribute(Request.java:1424)
        at
        org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:503)
        at
        javax.servlet.ServletRequestWrapper.setAttribute(ServletRequestWrapper.java:284)
        at
        com.ocpsoft.pretty.PrettyContext.setCurrentInstance(PrettyContext.java:93)
        at
        com.ocpsoft.pretty.PrettyContext.getCurrentInstance(PrettyContext.java:84)
        at
        com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:58)
        at
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at
        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at
        com.ocpsoft.socialpm.jsf.filter.DataSetupFilter.doFilter(DataSetupFilter.java:255)
        at
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
        at
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        at
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at
        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
        at
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
        at
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        at
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)


Where and how should I start trying to debug these random NPEs?


Thanks,
Lincoln