Nullpointerexception java
首先,我是一名经验丰富的程序员,但对Java知之甚少。八年前,我在这方面有两年的经验 我在以下代码中得到一个NullPointerException:Nullpointerexception java,java,nullpointerexception,Java,Nullpointerexception,首先,我是一名经验丰富的程序员,但对Java知之甚少。八年前,我在这方面有两年的经验 我在以下代码中得到一个NullPointerException: public static void handle(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException { Response gfexResponse = null; try {
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);
}
}
}
这是StackTrace:
[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)
stacktrace指向此行:
if(gfexResponse.getMessage()!= null){ <-------- this line
request.setAttribute("message", gfexResponse.getMessage());
}
if(gfexResponse.getMessage()!=null){该错误表明gfexResponse
对象本身是null
(即action.execute(event)
在上面的代码中返回null
,并且没有引发异常)实际上问题在于gfexResponse=action.execute>行(事件);
下面一行中获得NPE的唯一机会是gfexResponse
为空
if(gfexResponse.getMessage()!= null){ <-------- this line
如果(gfexResponse.getMessage()!=null){您确定gfexResponse
从action.execute(event);
(在try{}
中)获取实际值吗?我猜action.execute(event);
返回null。您的基本轮廓如下:
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.
}
}
为了解决眼前的问题-您的操作.执行(事件);
调用可能返回null
。但是,可以通过以下几种方式缓解此问题:
- 空检查,或
- 将
try-catch
块转换为它自己的单独方法调用,以返回响应
从这里开始,finally
块成为方法的主要焦点,您可以检查null,而不必担心finally
添加检查以查看gfexResponse==null
。Java中的null指针异常通常很容易调试,因为您试图在null对象上调用方法ce捕获关于action.execute(事件)
。你的答案是唯一能说明为什么gfexResponse
为null
的答案。谢谢。我试过了,但还是在同一点上得到了相同的异常。你们搞定了,谢谢!我错了,它在同一点爆炸了。我想我必须弄清楚如何使用调试模式。你说得对,这是错误的Racce,@Nathaniel Ford。不幸的是,这是它最初的编码方式,管理层没有人会同意花费来修复所有这些。这只是一个小网站,但最初的开发者在任何地方都以同样的方式处理流控制。
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.
}
}