每当我尝试保存到springboot h2数据库时,它都会因以下错误而崩溃,我似乎无能为力。 我也附上了代码。 我春天刚来。

我在网上遵循了一些教程,但目前仍处于停滞状态。

2019-08-14 23:17:33.559信息6296 --- [nio-8080-exec-1] oaccC [Tomcat]。[localhost]。[/]:初始化Spring DispatcherServlet'dispatcherServlet'2019-08-14 23: 17:33.559 INFO 6296-[nio-8080-exec-1] osweb.servlet.DispatcherServlet:初始化Servlet'dispatcherServlet'2019-08-14 23:17:33.567 INFO 6296-[nio-8080-exec- 1] osweb.servlet.DispatcherServlet:在8毫秒内完成初始化2019-08-14 23:17:33.607错误6296 --- [nio-8080-exec-1] oaccC [。[。[/]。[dispatcherServlet]:路径为[]的Servlet [dispatcherServlet]的Servlet.service()抛出异常[请求处理失败; 嵌套异常是java.lang.NullPointerException],其根本原因是

java.lang.NullPointerException:在com.test.lottery.lottery.LotteryController.CreateTicket(LotteryController.java:36)〜[classes /:na]在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本地方法)处为null )〜[na:na] at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:na] at java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:43)〜[na:na] at java.base / java.lang.reflect.Method.invoke(Method.java:567)〜[na:na] at org.springframework.web.method.support.InvocableHandlerMethod .doInvoke(InvocableHandlerMethod.java:190)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) 〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)〜[spring-webmvc -5 .1.9.RELEASE.jar:5.1.9.RELEASE],位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)〜[spring-webmvc-5.1.9.RELEASE。 jar:5.1.9.RELEASE]位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)〜[spring-webmvc-5.1.9.RELEASE.jar:5.1.9 .RELEASE]在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.springframework .web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)〜[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet。 java:942)〜[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)〜[spring-webmvc-5.1 .9.RELEASE.jar:5.1.9 [RELEASE],位于org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)〜[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE],位于javax.servlet.http.HttpServlet .service(HttpServlet.java:634)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)〜[spring- webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)〜[tomcat-embed-core-9.0.22.jar:9.0.22 ] org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)〜[tomcat-embed-core-9.0.22.jar:9.0.22] at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)〜[tomcat- embed-websocket-9.0.22.jar:9.0.22]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0 .22.jar:9.0.22],位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-9.0.22.jar:9.0.22],位于org.springframework .web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]位于org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter。 java:118)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core -org.apache.catalina.core.ApplicationFilterChain.doFilter(-ApplicationFilterChain.java:166)的-9.0.22.jar:9.0.22]〜org的[tomcat-embed-core-9.0.22.jar:9.0.22] .springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.springframework.web.filter.OncePerRequestFilter.doFilter( OncePerRequestFilter.java:118)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.a pache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.22.jar:9.0.22] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java :166)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)〜[spring-web-5.1.9。 RELEASE.jar:5.1.9.RELEASE],位于org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE] org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain)上的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.22.jar:9.0.22] .java:166)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)〜[spring-web-5.1。 9.RELEASE.jar:5.1.9.RELEASE]在org.springfra mework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)〜[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain .java:193)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core- 9.0.22.jar:9.0.22],位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)〜[tomcat-embed-core-9.0.22.jar:9.0.22],位于组织。 apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)〜[tomcat-embed-core-9.0.22.jar:9.0.22] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java :490)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)〜[tomcat-embed-core-9.0。 22.jar:9.0.22],位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)〜[tomcat-embed-core-9.0 .22.jar:9.0.22],位于org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)〜[tomcat-embed-core-9.0.22.jar:9.0.22],位于org.apache .catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java: 408)〜[tomcat-embed-core-9.0.22.jar:9.0.22] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)〜[tomcat-embed-core-9.0.22.jar :9.0.22],位于org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:853)〜[tomcat-embed-core-9.0.22.jar:9.0.22],位于org.apache.tomcat.util .net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1587)〜[tomcat-embed-core-9.0.22.jar:9.0.22]在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase。 java:49)〜[tomcat-embed-core-9.0.22.jar:9.0.22] at java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)〜[na:na] at java.base / java.util。 parallel.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:628)〜[na:na]在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)〜[tomcat-embed-core -9.0.22.jar:9.0.22],位于java.base / java.lang.Thread.run(Thread.java:835)〜[na:na]

编码

@Entity  public class TicketRequest {     int id;     Boolean checked;     Map<Integer, int[]> num_lines;       @Id       @GeneratedValue(strategy=GenerationType.AUTO)      @Column(name = "id")   public int getId(){     return id; }      @Column(name = "checked")   public Boolean getChecked(){     return checked; }      @ElementCollection     @Column(name = "ticket_num")   public Map<Integer, int[]> getTicketNum(){     return num_lines; }  public void setId(int id) {     this.id = id; }  public void setChecked(Boolean checked) {     this.checked = checked; }  public void setTicketNum(Map<Integer, int[]> num_lines){     this.num_lines = num_lines; } 

@RestController的代码:

@RestController public class LotteryController {         private TicketRepository TicketRepository;    @RequestMapping("/ticket")   public TicketRequest CreateTicket(@RequestParam int line) {        Map<Integer, int[]> lottery_lines = new HashMap<Integer,  int[]>();        for(int i = 1; i<= line; i++) {           int[] ints = new Random().ints(0, 3).distinct().limit(3).toArray();           lottery_lines.put(i,  ints);       // System.out.println("The Value are: " + Arrays.toString(lottery_lines.get(0)));      }         TicketRequest response = new TicketRequest();       response.setId(1);       response.setChecked(false);       response.setTicketNum(lottery_lines);         TicketRepository.save(response);      return response;    }  } 
public interface TicketRepository extends CrudRepository<TicketRequest, Integer>{  } 

===============>>#1 票数:0

尝试在控制器中为TicketRepository添加@Autowired批注。

似乎控制器中的TicketRepository为null,因为您没有将TicketRepository作为bean注入。

  ask by Joe translate from so

===============>>#2 票数:0

通过查看该堆栈跟踪,该错误与H2无关,您在控制器的第36行上获得了空指针。 不幸的是,我无法从您的代码中分辨出哪一行是数字36。

仔细看一下第36行,尝试找出可能为空的内容。

我的猜测是第36行是TicketRepository.save(response); TicketRepository从未在您的代码中初始化。 您可能只是在该字段上缺少@Autowire

  ask by Joe translate from so

===============>>#3 票数:0

LotteryController中的TicketRepository TicketRepository需要@Autowired表示法。

@RestController public class LotteryController {      @Autowired                private TicketRepository ticketRepository;      @RequestMapping("/ticket")     public TicketRequest CreateTicket(@RequestParam int line) {        Map<Integer, int[]> lottery_lines = new HashMap<Integer,  int[]>();        for(int i = 1; i<= line; i++) {           int[] ints = new Random().ints(0, 3).distinct().limit(3).toArray();           lottery_lines.put(i,  ints);       // System.out.println("The Value are: " + Arrays.toString(lottery_lines.get(0)));      }         TicketRequest response = new TicketRequest();       response.setId(1);       response.setChecked(false);       response.setTicketNum(lottery_lines);         ticketRepository.save(response);      return response;    }  } 

Spring容器负责管理Bean,当您使用@Autowired注释进行注释时,它将注入TicketRepository Bean。

  ask by Joe translate from so

本文未有回复,本站智能推荐: