Исключение нулевого указателя в Spring MVC Project - PullRequest
0 голосов
/ 04 октября 2019

Я создаю форму, которая содержит поля профессора, это поля id (long), nome (string), username (string), email (string) и curso_coord (curso), поскольку curso являетсяссылочная переменная для курса класса.

В форме вводятся поля имени пользователя, имени и адреса электронной почты, которые пользователь должен заполнить, и курс выбирается пользователем для выбора курса, если он / онакоординатор этого курса или -1, если нет

Я пытаюсь решить проблему с помощью curso.get (0) .getId (), но этого недостаточно

@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<Curso> 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;
    }

 ....
}

<form th:action="@{/professor}" th:object = "${professor}" method="POST">

            <div class = "form-group">
                <label for = "username">Username</label>
                <input type = "text" th:field = "*{username}" autofocus = "autofocus"/>
            </div>
            <div class = "form-group">
                    <label for = "nome">Nome</label>
                    <input type = "text" th:field = "*{nome}" autofocus = "autofocus"/>
            </div>
            <div class = "form-group">
                    <label for = "email">Email</label>
                    <input type = "hidden" name="email" value="">
                    <input type = "text" th:field = "*{email}" autofocus = "autofocus"/>
            </div>
            <div class = "form-group">
                <select th:field = "*{curso_coord}">
                    <option 
                    th:each = "c: ${cursos}"
                    th:value = "${c.id}">Cursos
                    </option>
                    <option value = -1> Não Coordenador
                    </option>
                </select>"
            </div>
            <button type = "submit" class = "btn btn-success ">Salvar</button>
        </form>

2019-10-04 15: 37: 00.700 ОШИБКА 17580 --- [nio-8081-exec-1] cppcontroller.ErrorController: Algum erro ocorreu!

java.lang.NullPointerException: пусто в com.planoensino.planoensino.controller.Prof ProfessorController.prof ProfessorInicio (ProfessorController.java:35) ~ [main /: na] в sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод) ~ [na: 1.8.0_181] в sun.reflect.NativeMethodAccessorImpl.etj (Invo.Ivj. : 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] в организации.springframework.web.method.support.InvocableHandlerMethod.doInvoke (InvocableHandlerMethod.java:209) ~ [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.method. InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod.java:136) ~ [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] по адресу org.springframework.web.servlet.mvc.method.annotation.HoldMableHervHableВысленныйВысленныйЗащитныйВозвратникТрань.java: 102) ~ [spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod (RequestMappingHandlerAd8ter). ~ [spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] по адресу org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapte)r.java:783) ~ [spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:991) ~ [spring-webmvc-5.0.8.RELEASE. jar: 5.0.8.RELEASE] в org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java:925) ~ [spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в орг. springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:974) [spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.servlet.FrameworkServlet.ervlet. java: 866) [spring-webmvc-5.0.8.RELEASE.jar: 5.0.8.RELEASE] по адресу 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] на javax.servlet.http.HttpServlet.service (HttpServlet.java:790) [javax.servlet-api-3.1.0.jar: 3.1.0] на org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:6)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]в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain. (FilterChainProxy.java:320) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke (FilterSecurityInterceptor.java:127) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в 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]на org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.session.SessionManagementFilter.doFilter (SessionManagementFilter.java:137) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:33) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в 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] в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter (RequestCacheAwareFilter.java:63) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.FilterChainChainProha $.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter (AbstractAuthenticationProjj) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.authentication.logout.LogoutFilter.doFilter (LogoutFilter.java:116) [spring-security-web-5.0.7. RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7. RELEASE] на org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal (HeaderWriterFilter.java:66) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] на org.springframework.web.filter. : 334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE]в org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter (SecurityContextPersistenceFilter.java:105) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security. .FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.context.request.async.WebAsyncManager.doFilterInternal (WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestF10). spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) [spring-security-web-5.0.7. RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.FilterChainProxy.doFilterInternal (FilterChainProxy.java:215) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.security.web.FilterChainProxy.doFilter (FilterChainProxy.java:178) [spring-security-web-5.0.7.RELEASE.jar: 5.0.7.RELEASE] в org.springframework.web.filter. DelegatingFilterProxy.invokeDelegate (DelegatingFilterProxy.java:357) [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.filter.DelegatingFilterProxy.doFilter (DelegatingFilterProxy.java:2-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] в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.springframework.web.filter.RequestContextFilter. doFilterInternal (RequestContextFilter.java:99) [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107)-5.0.8.RELEASE.jar: 5.0.8.RELEASE] на орг.аpache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterCha. 166) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal (HttpPutFormContentFilter.java:109) [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107) [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.apache. catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterCha6.j::[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] в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107) [spring-web-5.0.8.RELEASE.jar: 5.0.8.RELEASE] в org.apache.catalina.core.ApplicationFilterChain.internalDoFilter. java: 193) [tomcat-embed-core-8.5.32.jar: 8.5.32] в 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] вorg.springframework.session.web.http.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:81)[spring-session-core-2.0.5.RELEASE.jar: 2.0.5.RELEASE] в 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] на org.springframework.web. filter.CharacterEncodingFilter. [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] в org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core. StandardWrapperValve.invoke (StandardWrapperValve.java:198) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:96) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina. authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:493) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:140) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:81) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:87) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:342) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.coyote.http11.Http11Processor.service (Http11Processor.java:800) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.coyote. AbstractProtocol $ ConnectionHandler.process (AbstractProtocol.java:800) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun (NioEndpoint.java:1471) [tomcat-embed-core-8.5.32.jar: 8.5.32] в org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java:49) [tomcat-embed-core-8.5.32.jar: 8.5.32] в java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149) [na: 1.8.0_181] в java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:6)[na: 1.8.0_181] в org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread.java:61) [tomcat-embed-core-8.5.32.jar: 8.5.32] в java. lang.Thread.run (Thread.java:748) [na: 1.8.0_181]

2019-10-04 15: 37: 00.702 WARN 17580 --- [nio-8081-exec-1]. mmaExceptionHandlerExceptionResolver: разрешенное исключение, вызванное выполнением обработчика: java.lang.NullPointerException

Ответы [ 3 ]

0 голосов
/ 04 октября 2019

Отсутствует добавление @Autowired annotation для CursoRepository, что является причиной исключения нулевого указателя.

@Autowired
CursoRepository c;
0 голосов
/ 04 октября 2019

Вы забыли поставить @Autowired на CursoRepository c

Просто измените код, заменив существующий:

@Autowired
CursoRepository c;
0 голосов
/ 04 октября 2019
CursoRepository c;

не является управляемым компонентом Spring, попробуйте

@Autowired
ProfessorRepository pr;

@Autowired
CursoRepository c;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...