7 Replies Latest reply on Jul 13, 2010 2:10 AM by LIM BY

    java.lang.NullPointerException: FacesContext is null

    LIM BY Newbie

      I set timeout in web.xml as below:

      <session-config>
            <session-timeout>1</session-timeout>
        </session-config>

       

      and I did the filter as below:

      HttpServletRequest hreq = (HttpServletRequest) request;
              HttpSession session = hreq.getSession(true);
              User adminUser = (User) session.getAttribute(FacesUtil.SESSION_USER);

       

              // ----- Check if user has logged in -----
              if (adminUser == null) {
                  filterConfig.getServletContext().getRequestDispatcher(
                          NOT_LOGIN_PAGE).forward(request, response);
                  return;
              }

       

       

      I login and let the page IDE for 2 minutes, after 2 minutes, when I click for next action, the error as below appear.

      SEVERE: Servlet.service() for servlet jsp threw exception
      java.lang.NullPointerException: FacesContext is null
          at org.ajax4jsf.context.AjaxContext.getCurrentInstance(AjaxContext.java:159)
          at org.ajax4jsf.context.AjaxContext.getCurrentInstance(AjaxContext.java:144)
          at org.ajax4jsf.component.AjaxViewRoot.getViewId(AjaxViewRoot.java:588)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:104)
          at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:530)
          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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
          at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
          at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
          at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
          at my.com.by.project.ui.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:48)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:530)
          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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
          at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
          at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
          at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
          at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:410)
          at com.sun.faces.application.ViewHandlerImpl.executePageToBuildView(ViewHandlerImpl.java:468)
          at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:140)
          at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
          at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
          at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
          at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:530)
          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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
          at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
          at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
          at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
          at my.com.by.project.ui.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:48)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:530)
          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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
          at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
          at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
          at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
          at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:410)
          at com.sun.faces.application.ViewHandlerImpl.executePageToBuildView(ViewHandlerImpl.java:468)
          at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:140)
          at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
          at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
          at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
          at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:530)
          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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
          at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
          at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
          at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
          at my.com.by.project.ui.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:48)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:2

      .......

       

       

      May I know how to solve this problem?

        • 1. Re: java.lang.NullPointerException: FacesContext is null
          LIM BY Newbie

          I found something weird about this issue. I try to put System.out.println within the code as below in red.

           

          HttpServletRequest hreq = (HttpServletRequest) request;
          HttpSession session = hreq.getSession(true);
          User adminUser =  (User) session.getAttribute(FacesUtil.SESSION_USER);

           

          String servletPath1 = hreq.getServletPath();
          System.out.println("path : " + servletPath1);

           

          //  ----- Check if user has logged in -----
          if (adminUser ==  null) {
               filterConfig.getServletContext().getRequestDispatcher(
                     NOT_LOGIN_PAGE).forward(request, response);
              return;

          }

           

          After timeout period, I try to access another page, then it keep reload the same page, so the console will keep looping the path as below:

           

          path : /admin/adminmain.jsf

          path : /admin/adminmain.jsf

          path : /admin/adminmain.jsf

          path : /admin/adminmain.jsf

          .....................

           

          and at the end the console will show me the problem as my previous post.

          May I know how to fix this problem?

          Thanks.

          • 2. Re: java.lang.NullPointerException: FacesContext is null
            Nick Belaevski Master

            Hi,

             

            I see custom AuthorizationFilter is used. Maybe it's causing infinite loop?

            • 3. Re: java.lang.NullPointerException: FacesContext is null
              LIM BY Newbie

              Thanks for your reply. Anyway, I don't think is cause by the AuthorizationFilter, below is the code for AuthorizationFilter:

               

              HttpServletRequest hreq = (HttpServletRequest) request;
              HttpSession session = hreq.getSession(true);
              User adminUser = (User) session.getAttribute(FacesUtil.SESSION_USER_DETAIL);

               

              String servletPath1 =  hreq.getServletPath();
              System.out.println("path : " + servletPath1);

               

              // ----- Check if user has logged in -----
              if (adminUser == null) {
                  System.out.println("adminUser : " + adminUser);
                  filterConfig.getServletContext().getRequestDispatcher(
                          NOT_LOGIN_PAGE).forward(request, response);
                  return;
              }

               

              System.out.println("----- after -------");

               

              ......................

               

              According to the code, when I try to access to another page after timeout, the adminUser is NULL, so it will forward me to NOT_LOGIN_PAGE page, but it didn't. The system just keep looping in the same page, and the console showing the message as below:

               

              path : /admin/adminmain.jsf

              adminUser : null

              path : /admin/adminmain.jsf

              adminUser : null
              path : /admin/adminmain.jsf

              adminUser : null

              path : /admin/adminmain.jsf

              adminUser : null

              path :  /admin/adminmain.jsf

              adminUser : null
              path : /admin/adminmain.jsf

              adminUser : null

              ........

               

              If you notice, the message -----  after ------- never showing in console.

               

              I wonder why this happen? Please advice.

              • 4. Re: java.lang.NullPointerException: FacesContext is null
                praveen jain Novice

                try to print filterConfig it should be null.

                how u are getting filterConfig? check whether it is initialized properly.

                • 5. Re: java.lang.NullPointerException: FacesContext is null
                  LIM BY Newbie

                  Below is the whole code for AuthorizationFilter. Anyway, I try the situation for "// -----  Check if user has access rights to function -----", it work properly, the system will forward to the ACCESS_DENIED_PAGE page, but the "//  ----- TIMEOUT -----" session will trigger problem.

                   

                  I had initial the filterConfig in init method.

                   

                  ---------------------------------

                  public class AuthorizationFilter implements Filter {

                   

                      private final String ACCESS_DENIED_PAGE = "/NoAccess.jsf";
                      private final String NOT_LOGIN_PAGE = "/NoLogin.jsf";

                   

                      private FilterConfig filterConfig = null;

                   

                      /**
                       * @see Filter#destroy()
                       */
                      public void destroy() {
                          // TODO Auto-generated method stub
                      }

                   

                      /**
                       * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
                       */
                      public void doFilter(ServletRequest request, ServletResponse response,
                              FilterChain chain) throws IOException, ServletException {
                          HttpServletRequest hreq = (HttpServletRequest) request;
                          HttpSession session = hreq.getSession(true);
                          User adminUser = (User) session.getAttribute(FacesUtil.SESSION_USER_DETAIL);

                   

                          // ----- TIMEOUT -----
                          if (adminUser == null) {
                              filterConfig.getServletContext().getRequestDispatcher(
                                      NOT_LOGIN_PAGE).forward(request, response);
                              return;
                          }
                         
                         // ----- Check if user has access rights to function -----
                          String funcPath = SysPath.getPath();

                   

                          Map<String, String> accessRightMap = adminUser.getAccessRightMap();
                          if (!accessRightMap.containsValue(funcPath)) {
                              filterConfig.getServletContext().getRequestDispatcher(
                                      ACCESS_DENIED_PAGE).forward(request, response);
                              session.invalidate();
                              return;
                          }
                         
                          // pass the request along the filter chain
                          chain.doFilter(request, response);
                      }

                   

                      /**
                       * @see Filter#init(FilterConfig)
                       */
                      public void init(FilterConfig filterConfig) throws ServletException {
                          this.filterConfig = filterConfig;
                      }

                   

                  }

                  ---------------------------------------------

                   

                  Please advise.

                  • 6. Re: java.lang.NullPointerException: FacesContext is null
                    praveen jain Novice

                    i have seen this line

                     

                    private FilterConfig filterConfig = null;

                     

                    and than

                     

                    filterConfig.getServletContext().getRequestDispatcher

                     

                    but i dint find any line which should use to initialize the filterConfig.

                     

                    it musst be initialised before using , if you try to run

                    System.out.println("filterConfig   "+filterConfig);

                    you will get NULL.

                    • 7. Re: java.lang.NullPointerException: FacesContext is null
                      LIM BY Newbie

                      The initial for filterConfig is locate at the bottom of the code in init method, you refer to the whole code or the part of the code as below:

                       

                      /**
                        * @see Filter#init(FilterConfig)
                        */
                          public  void init(FilterConfig filterConfig) throws ServletException {
                              this.filterConfig = filterConfig;
                          }

                       

                      Sorry if I didn't highlight it properly.

                       

                      Appreciate your help.