2013-06-18 69 views
7

首先,我是一名經驗豐富的程序員,但對Java幾乎不熟悉。八年前,我有大約兩年的經驗。Nullpointerexception java

我得到下面的代碼一個NullPointerException:

public static void handle(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException { 

    Response gfexResponse = null; 

    try { 

     ActionFactory actionFactory = ActionFactory.getInstance(); 
     String requestURL = request.getRequestURI(); 
     String actionId = actionFactory.getActionId(requestURL); 

     IAction action = actionFactory.createAction(actionId); 

     ActionEvent event = new ActionEvent(request, 0, actionId); 
     gfexResponse = action.execute(event); 

    } catch (Exception ex) {   
     gfexResponse = new Response(); 
     gfexResponse.setError(ex.getMessage()); 
     gfexResponse.setOutcome(IViewConstants.ERROR); 

    } finally { 

     if(request.getParameter("loginId") != null){ 
      request.setAttribute("loginId", request.getParameter("loginId")); 
     } 

     if(gfexResponse.getMessage()!= null){ 
      request.setAttribute("message", gfexResponse.getMessage()); 
     } 

     if(gfexResponse.getError()!= null){ 
      request.setAttribute("error", gfexResponse.getError()); 
     } 

     if (gfexResponse.getContentType() != null) { 
      response.setContentType(gfexResponse.getContentType()); 
      OutputStream outputStream = response.getOutputStream(); 
      outputStream.write(gfexResponse.getOutputData()); 
      outputStream.flush(); 
      outputStream.close(); 
     } 

     if(gfexResponse.getOutcome() != null){ 
      RequestDispatcher dispatcher = request.getRequestDispatcher(gfexResponse.getOutcome()); 
      dispatcher.forward(request, response); 
     } 
    }  
} 

這裏的堆棧跟蹤:

[6/18/13 17:10:04:518 GMT] 00000023 ServletWrappe E SRVE0068E: Uncaught exception thrown in one of the service methods of the servlet: GfexServlet. Exception thrown : java.lang.NullPointerException 
    at com.svl.gfex.handlers.RequestHandler.handle(RequestHandler.java:44) 
    at com.svl.gfex.servlets.GfexServlet.processRequest(GfexServlet.java:43) 
    at com.svl.gfex.servlets.GfexServlet.doPost(GfexServlet.java:39) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:763) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) 
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:966) 
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:907) 
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:118) 
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87) 
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:701) 
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:646) 
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:475) 
    at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:463) 
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3129) 
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:238) 
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:811) 
    at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1433) 
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:93) 
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465) 
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:394) 
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102) 
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:152) 
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:213) 
    at com.ibm.io.async.AbstractAsyncFuture.fireCompletionActions(AbstractAsyncFuture.java:195) 
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136) 
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:194) 
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:741) 
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:863) 
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510) 

堆棧跟蹤指向該行:

 if(gfexResponse.getMessage()!= null){ <-------- this line 
      request.setAttribute("message", gfexResponse.getMessage()); 
     } 

此代碼是由離岸承包商維護,但公司解僱了所有承包商。爲了我的罪,我得到了修理它的工作。

如果有人可以幫我弄清楚爲什麼我得到這個錯誤,我將不勝感激。

+2

添加檢查以查看'gfexResponse == null'。 Java中的空指針異常通常非常容易調試,因爲您試圖在空對象上調用方法。 – 2013-06-18 18:46:00

回答

11

這錯誤表明gfexResponse對象本身是null(即action.execute(event)將返回在上面的代碼null,並沒有異常被拋出)

+1

+1關於'action.execute(event)'的好消息。你的答案是唯一一個說明爲什麼'gfexResponse'是'null' .. –

1

其實問題就出在該行gfexResponse = action.execute(event);

唯一的機會在下面的一線得到NPE這裏是gfexResponse爲null

if(gfexResponse.getMessage()!= null){ <-------- this line 

將其更改爲

if(gfexResponse!=null && gfexResponse.getMessage()!= null){ <-------- this line 
+0

謝謝。我試了一下,仍然在同一點得到同樣的例外。 – Kevin

+0

你們釘了它,謝謝!我錯誤地認爲它在同一時間爆炸。 – Kevin

1

你肯定gfexResponse越來越(在try {})從action.execute(event);的實際價值?我猜想action.execute(event);返回null。

+0

我想我必須弄清楚如何使用調試模式。 – Kevin

4

你的基本輪廓是這樣的:

public static void handle(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException { 

    Response gfexResponse = null; 

    try { 
     //Try to get your gfexResponse 
    } catch (Exception ex) {   
     //Provide a default gfexResponse 
    } finally { 
     //Do some stuff with gfexResponse 
    }  
} 

這是不好的做法:你嘗試使用異常處理流程控制。此外,假設您使用獲取gfexResponse 的方法將在失敗時拋出異常,這顯然不是。 (一些簡單的調試/跟蹤將直接顯示這一點。)

你應該做的是以下幾點:

public static void handle(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException { 

    Response gfexResponse = null; 

    try { 
     //Try to get your gfexResponse 
     //Make sure you got your response object and throw SomeAppropriateException if not 
     //Do some stuff with gfexResponse 
    } catch (SomeAppropriateException e) { 
     //properly handle this case 
    } catch (Exception ex) { 
     //properly handle the general case that something else failed (But you should try to be more specific) 
    } finally { 
     //remove any resources that might not be properly cleaned up if an exception is thrown. 
    }  
} 
+0

@Nathaniel Ford,你說得對,這是不好的做法。不幸的是,這就是它原來的編碼方式,管理層中沒有人會花費這些來解決所有問題。這只是一個小型網站,但原始開發人員在任何地方都以相同的方式處理流量控制。 – Kevin

1

爲了解決眼前的痛苦 - 你action.execute(event);呼叫可能返回null。然而,這可以通過多種方式來緩解:

  • 空檢查,或
  • 開啓try-catch塊到它自己單獨的方法調用,返回Response

從那裏,finally塊成爲你的方法的主要焦點,你可以檢查空,而不必擔心finally