2014-03-12 111 views
1

我在web.xml部署描述符中添加了一個過濾器。但是隻要有請求來服務器,過濾器會拋出一個NullPointerExceptionFilterChain doFilter中的java.lang.NullPointerException方法

java.lang.NullPointerException 
org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:427) 
org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:340) 
org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:664) 
org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715) 
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:884) 
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811) 
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298) 
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252) 
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:821) 
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254) 
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482) 
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

下面給出的代碼段,其中的例外occures。

public void doFilter(ServletRequest request, ServletResponse response, 
        FilterChain chain)  throws IOException, ServletException { 

    HttpServletRequest httpRequest = (HttpServletRequest) request; 
    HttpServletResponse httpResponse = (HttpServletResponse) response; 

    boolean isAjax = "XMLHttpRequest".equals(httpRequest.getHeader("x-requested-with")); 


    HttpSession httpSession = httpRequest.getSession(false); 

    if(isAjax){ 
     if(isValidRequest(httpRequest, httpSession)){ 
      chain.doFilter(request, response); 
     }else{ 
      returnInvalidMessage(httpRequest, httpResponse); 
     } 
    }else{ 
     chain.doFilter(request, response); /* Here is the exception occures */ 
    } 
} 

異常在下面的線

chain.doFilter(request, response);

是任何一個有這個想法發生?

+0

這不是拋出'NullPointerException'的行。你必須瞭解堆棧跟蹤的內容。請發佈整個堆棧跟蹤。 –

+0

@LuiggiMendoza我再次檢查它。但是我在'com.incitesys.common.controller.SessionHandlingFilterAjax.doFilter(SessionHandlingFilterAjax.java:94)'中發現了NPE。行號94包含chain.doFilter(request,response); –

回答

1

更改爲

String h = httpRequest.getHeader("X-Requested-With"); 
boolean isAjax = (h == null?false:h.indexOf("XMLHttpRequest")>=0); 

isAjax進行評估,以true如果這是一個Ajax請求。

+0

@sakura如果這不是有效的Ajax請求表單bean不應該填充。 –

+0

感謝您的回覆。我檢查了你的邏輯。但是在'chain.doFilter(request,response);'行中提出的實際問題。通過處理這個事情,一些東西變成了'null'。 'chain'是'javax.servlet.FilterChain'的引用變量。 –

+0

我得到了解決方案。實際上問題與過濾器沒有關聯。我在'session'中添加了一個'ActionForm'。當ActionForm對象從會話範圍中移除時拋出'NPE'。 –

0

如果你的項目在tomcat上運行,可能是你的項目jar的問題。

的jsp-api.jar文件和servlet-api.jar文件

是衝突到Tomcat的罐子。所以只刪除jar

相關問題