0

Java 7, JSP, Spring 3, Apache-Tomcat 7.0.63. Netbeans.

I made changes to an existing JSP file, and changed the backing form object. I went through the JSP to ensure that all references were backed by a field of the correct name in the backing form object.

In the controller, I have a private saveEditedOrNewItem(); it invokes a method to initialize variables in the model for the page - setCommonModelAttributesForEdit(). The program bombs when the former calls the latter, throwing a NullPointerException, but I'm not dereferencing anything, I am making no calls within the call to get parameters or anything. One of the parameters is null, but that would not cause NPE by itself.

I have a breakpoint on the first executable line of setCommonModelAttributesForEdit(); that breakpoint is not reached when the error is triggered (though it executes on a previous invocation and runs to completion without error, so that method is executable).

Here's the code that throws the error:

    if (errors.hasErrors()) {
        if (entityCode == null || entityCode.equals("")) { entityCode = "Select"; }
        Integer entityId = prospectForm.getentityId();
        String prospectName = prospectForm.getName();
        Object leafNodeFlag = getLeafNodeFlag(request);
        Boolean bLeafNodeFlag = (Boolean)leafNodeFlag;
        boolean bbLeafNodeFlag = (boolean)bLeafNodeFlag;
        Integer logoAttachmentId = prospectForm.getLogoAttachmentId();
        ModelMap localModelMap = model;
        // the following is line 474 in the stacktrace
        setCommonModelAttributesForEdit(localModelMap, entityCode, entityId,
                                        prospectName, bbLeafNodeFlag, prospectForm,
                                        logoAttachmentId);
        return prospectEditView;

And here's the error stacktrace:

java.lang.NullPointerException
  at com.accesspointinc.crm.prospect.MyFormController.saveEditedOrNewProspect(MyFormController.java:474)
  at com.accesspointinc.crm.prospect.MyFormController.saveNewProspect(MyFormController.java:434)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at com.accesspointinc.commons.filter.SessionTimeoutCookieFilter.doFilter(SessionTimeoutCookieFilter.java:40)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at com.accesspointinc.commons.filter.StatesFilter.doFilter(StatesFilter.java:34)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
  at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:322)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2516)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2505)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:722)

I 'flattened' all the parameters passed to setCommonModelAttributesForEdit(); I have a breakpoint on the call, and can examine all the values being passed in. I even ensured I wouldn't have to cast within the method call. The logoAttachmentId is null, the rest have non-null values. To repeat, a breakpoint set at the start of setCommonModelAttributesForEdit() is not hit on this invocation, though it is hit on an earlier invocation.

If I do "step into" instead of "step over" for the call, I end up in an InvocationTargetException constructor with a target of NullPointerException; the call stack indicates (eventually) that I am in a call from SessionTimeoutCookieFilter.doFilter(); the class is an extension of GenericFilterBean, and the line where it is failing is filterChain.doFilter(request, response);

So it is not your mothers NullPointerException. I am not dereferencing anything in my code, and it halts when I attempt to call a private method within a class in which I'm already executing, so the object for which the method is being referenced is already instantiated. Neither of these are static methods. I have done "clean and compile" multiple times, I have restarted NetBeans, I have rebooted Windows, I have cleaned my browser's cache.

Can anyone suggest what else I might do to find this problem?


Edit: I don't think this qualifies as a duplicate of the question for which it has been closed as a duplicate. Just because a question involves a null pointer does not mean it is a duplicate of the Big "Null Pointer" question.

The cause of this NPE (autobox conversion) was mentioned once in its supposed duplicate, in a comment, and in a different context than it occurred in this question. The current question turned out to be answered by autoboxing as part of parameter passing to a method call; the only mention of it in the Big Question was in an assignment statement. I should have figured it out in less time than it took me, but it stumped a lot of other people, including everyone who read it on SO. (It only got marked as a duplicate after it had been answered.)

I looked through the Big Null Pointer Question before I posted this, and the answer to my question is not there.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • What is there on line number 474? MyFormController.java:474 – faheem farhan Jul 26 '16 at 18:29
  • It is the call to setCommonModelAttributesForEdit; thanks for pointing out that I had not said, I've now edited the original to indicate where it is. – arcy Jul 26 '16 at 18:36
  • There's no magic: there's no way line 474 is where you think it is, the compiled code and the source in your IDE got out of sync somehow. My first instinct would be to fire up a debugger and watch the state after every line to verify what's going on. – kryger Jul 27 '16 at 07:59
  • @kryger Yes, there is a way 474 is where the runtime and I both think it is. I was getting puzzled (and frustrated enough) to doubt the tools also. I'm not sure what you mean by "watch(ing) the state", I don't know what 'state' you mean. I did find the problem, and provided an answer below. – arcy Jul 27 '16 at 09:48
  • Interesting to learn this, I can imagine knowing something like this can save one's neck and earn colleagues' admiration. – kryger Jul 27 '16 at 10:39

2 Answers2

0

Could it be possible that 1.) Your session is expiring while you are debugging? 2.) You controller is invoked twice (perhaps more times) and the first time the expected values are there while they don't exist in subsequent calls?

earmitage
  • 1
  • 1
0

It turns out that the last parameter to setCommonModelAttributesForEdit() is declared as int, and the value being passed in is a null. It worked the previous time when the Integer passed in for this parameter had a value, but autoboxing (evidently) fails with a NullPointerException when a null is passed in.

arcy
  • 12,845
  • 12
  • 58
  • 103