title: NullPointerException in Spring Controller
body: <p>I have simple Spring MVC application. When I try to access </p>
  
  <blockquote>
    <p><a href="http://localhost:8080/paygate/transaction" rel="nofollow">http://localhost:8080/paygate/transaction</a> </p>
  </blockquote>
  
  <p>I get an error:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
  org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
  org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
  org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
  org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
  
  
  root cause 
  
  java.lang.NullPointerException
  net.ggonchar.paygate.web.FrontController.runTransactionPost(FrontController.java:52)
  net.ggonchar.paygate.web.FrontController.runTransactionGet(FrontController.java:60)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  java.lang.reflect.Method.invoke(Method.java:597)
  org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
  org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
  org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
  org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
  org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
  org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
  org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
  org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
  </code></pre>
  
  <p>My web.xml:</p>
  
  <pre><code>    &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>servlet-context.xml:</p>
  
  <pre><code>    &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
  &lt;annotation-driven /&gt;
      &lt;!-- Imports user-defined @Controller beans that process client requests --&gt;
  &lt;beans:import resource="controllers.xml" /&gt;
  </code></pre>
  
  <p>controllers.xml</p>
  
  <pre><code>    &lt;!-- Scans within the base package of the application for @Components to configure as beans --&gt;
  &lt;context:component-scan base-package="net.myapp.paygate.web" /&gt;
  </code></pre>
  
  <p>FrontController.java:</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  public class FrontController {
  
  private LogService logService;
  private TransactionService transactionService;
  
  @Autowired
  public FrontController(LogService logService) {
      this.logService = logService;
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public String viewLogsGet(Model model) {
      List&lt;Log&gt; logs = java.util.Collections.emptyList();
      logs = logService.query();
      model.addAttribute("logs",logs);
      return "viewLogs";
  }
  
  @RequestMapping(method = RequestMethod.POST)
  public String viewLogsPost(Model model) {
      return viewLogsGet(model);
  }
  
  @RequestMapping(value = "/transaction", method = RequestMethod.POST)
  public String runTransactionPost(/*Map&lt;String, Object&gt; map,*/
          @ModelAttribute("transaction") Transaction transaction,
          BindingResult result) {
  
      //map.put("transaction", new Transaction());
      if (transaction != null)
          transactionService.startTransaction(transaction);
      return "transaction";
  }
  
  @RequestMapping(value = "/transaction", method = RequestMethod.GET)
  public String runTransactionGet(Map&lt;String, Object&gt; map,
          @ModelAttribute("transaction") Transaction transaction,
          BindingResult result) {
      return runTransactionPost(/*map,*/transaction, result);
  }
  
  }
  </code></pre>