title: Null Pointer Exception in Spring MVC Project
body: <p>I'm creating a form that contains the fields of a professor, this fields are id (long), nome(string), username (string), email(string) and curso_coord(curso), being that curso is a reference variable for a class course.</p>
  
  <p>In the form, the username, name and email fields are entered for the user to fill in and the course is a select for the user to select the course if he / she is coordinator of that course or -1 if not</p>
  
  <p>I try curso.get(0).getId() to solve the problem but it's not enough</p>
  
  <pre><code>@Controller
  @RequestMapping(value = { "/professor" })
  public class ProfessorController {
      @Autowired
      ProfessorRepository pr;
      CursoRepository c;
  
      @GetMapping(value = "")
      @ResponseBody
      public ModelAndView professorInicio() {
          ModelAndView form = new ModelAndView("/form");
          form.addObject("professor", new Professor());
          List&lt;Curso&gt; curso = c.findAll();
          curso.get(0).getId();
          form.addObject("curso", curso);
          return form;
      }
  
      @PostMapping(value = "")
      @ResponseBody
      public ModelAndView cadastraProfessor(@ModelAttribute ("professor") Professor professor){
          //Professor prof = new Professor();
          ModelAndView index = new ModelAndView("/index");
          professor.getUsername();
          professor.getEmail();
          professor.getNome();
          professor.getCursoCoord();
          pr.save(professor);
  
          return index;
      }
  
   ....
  }
  
  &lt;form th:action="@{/professor}" th:object = "${professor}" method="POST"&gt;
  
              &lt;div class = "form-group"&gt;
                  &lt;label for = "username"&gt;Username&lt;/label&gt;
                  &lt;input type = "text" th:field = "*{username}" autofocus = "autofocus"/&gt;
              &lt;/div&gt;
              &lt;div class = "form-group"&gt;
                      &lt;label for = "nome"&gt;Nome&lt;/label&gt;
                      &lt;input type = "text" th:field = "*{nome}" autofocus = "autofocus"/&gt;
              &lt;/div&gt;
              &lt;div class = "form-group"&gt;
                      &lt;label for = "email"&gt;Email&lt;/label&gt;
                      &lt;input type = "hidden" name="email" value=""&gt;
                      &lt;input type = "text" th:field = "*{email}" autofocus = "autofocus"/&gt;
              &lt;/div&gt;
              &lt;div class = "form-group"&gt;
                  &lt;select th:field = "*{curso_coord}"&gt;
                      &lt;option 
                      th:each = "c: ${cursos}"
                      th:value = "${c.id}"&gt;Cursos
                      &lt;/option&gt;
                      &lt;option value = -1&gt; Não Coordenador
                      &lt;/option&gt;
                  &lt;/select&gt;"
              &lt;/div&gt;
              &lt;button type = "submit" class = "btn btn-success "&gt;Salvar&lt;/button&gt;
          &lt;/form&gt;
  </code></pre>
  
  <blockquote>
    <p>2019-10-04 15:37:00.700 ERROR 17580 --- [nio-8081-exec-1]
    c.p.p.controller.ErrorController         : Algum erro ocorreu!</p>
    
    <p>java.lang.NullPointerException: null
            at com.planoensino.planoensino.controller.ProfessorController.professorInicio(ProfessorController.java:35)
    ~[main/:na]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    ~[na:1.8.0_181]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    ~[na:1.8.0_181]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
            at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
    ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
    ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
    [spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
    [spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    [javax.servlet-api-3.1.0.jar:3.1.0]
            at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    [spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    [javax.servlet-api-3.1.0.jar:3.1.0]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    [tomcat-embed-websocket-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    [spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:147)
    [spring-session-core-2.0.5.RELEASE.jar:2.0.5.RELEASE]
            at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81)
    [spring-session-core-2.0.5.RELEASE.jar:2.0.5.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    [na:1.8.0_181]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    [na:1.8.0_181]
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    [tomcat-embed-core-8.5.32.jar:8.5.32]
            at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]</p>
    
    <p>2019-10-04 15:37:00.702  WARN 17580 --- [nio-8081-exec-1]
    .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused
    by Handler execution: java.lang.NullPointerException</p>
  </blockquote>