Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullpointerexception java

First off, I'm an experienced programmer, but have very little familiarity with Java. I have about two years of experience with it, eight years ago.

I'm getting a NullPointerException in the following code:

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);
        }
    }       
}

Here's the 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)

The stacktrace points to this line:

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

This code was being maintained by an offshore contractor, but the company laid off all the contractors. For my sins, I was given the job of fixing it.

If anyone can help me figure out why I'm getting this error, I would appreciate it.

like image 631
Kevin Avatar asked Jun 18 '13 18:06

Kevin


People also ask

What is a null pointer exception in Java?

Null Pointer Exception In Java. NullPointerException is a RuntimeException. In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from a null object.

What is Java 27 NullPointerException?

27 NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference 43 java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference

What is the hierarchy of NullPointerException?

Throwing a null object. The Null Pointer Exception extends from the class RuntimeException. The hierarchy of NullPointerException is given below. As shown in the above hierarchy, Null Pointer Exception extends from the RuntimeException that inherits the Exception Class.

What is NullPointerException in thread main?

Exception in thread "main" java.lang.NullPointerException The reason you are getting this error is because we are trying to perform the length () operation on str1 which is null.


3 Answers

That error indicates that the gfexResponse object itself is null (i.e. action.execute(event) is returning null in the code above, and no exception is being thrown)

like image 146
Drew Shafer Avatar answered Sep 28 '22 07:09

Drew Shafer


Your basic outline is this:

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
    }       
}

This is bad practice: you're attempting to use exception handling for flow control. Further, you assume that the method you use to get gfexResponse will throw an exception if it fails, which clearly it does not. (Some simple debugging/tracing will reveal this directly.)

What you should be doing is the following:

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.
    }       
}
like image 36
Nathaniel Ford Avatar answered Sep 28 '22 07:09

Nathaniel Ford


Are you sure that gfexResponse is getting an actual value from action.execute(event); (in the try {})? I'd guess that action.execute(event); is returning null.

like image 37
karllindmark Avatar answered Sep 28 '22 07:09

karllindmark