NullPointerException:尝试使用 jdbcTemplete 时为 null
NullPointerException: null when trying to use jdbcTemplete
我正在尝试 2 天来解决我的问题并了解我错在哪里。
我正在尝试使用 TodoService 在网格上显示项目并将数据添加到我的 SQL。
但我得到错误:
我只是在待办事项视图中的 4 个字段中输入内容,然后单击保存(הוסף) 按钮。
我猜我的服务有问题。但我不明白是什么。
人们试图帮助我,但似乎无济于事。
java.lang.NullPointerException: null
at com.packagename.ui.views.TodoView.saveTodo(TodoView.java:111) ~[classes/:na]
at com.packagename.ui.views.TodoView.onComponentEvent(TodoView.java:127) ~[classes/:na]
at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:205) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:373) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.component.ComponentEventBus.lambda$addDomTrigger$dd1b7957(ComponentEventBus.java:264) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda$fireEvent(ElementListenerMap.java:441) ~[flow-server-2.0.14.jar:2.0.14]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507) ~[na:na]
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:441) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:59) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:64) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:378) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations(ServerRpcHandler.java:359) ~[flow-server-2.0.14.jar:2.0.14]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507) ~[na:na]
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:359) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:301) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:87) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:95) ~[vaadin-spring-12.0.6.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) ~[javax.servlet-api-4.0.1.jar:4.0.1]
at 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]
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665) ~[javax.servlet-api-4.0.1.jar:4.0.1]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) ~[javax.servlet-api-4.0.1.jar:4.0.1]
at 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]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.22.jar:9.0.22]
at org.apache.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]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.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]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at 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]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at 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.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
TodoService.java
@Component
public class TodoService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Todo> findAll() {
return jdbcTemplate.query(
"SELECT ID, TaskStatus, dudate,subject FROM todo",
(rs, rowNum) -> new Todo(rs.getLong("ID"),
rs.getLong("UserID"), rs.getLong("descriptionID"),rs.getString("taskstatus")));
}
public void update(Todo todo) {
jdbcTemplate.update("INSERT INTO todo (TaskStatus,dudate,subject,CustomerID) VALUES(?,?,?,?)",todo.getTaskStatus(),todo.getDudate(),todo.getSubject(),todo.getCustomerID());
}
}
Todo.java
public class Todo {
private long userID;
private String taskstatus;
private long ID;
private long customerID;
private long descriptionID;
private String taskStatus;
private String Subject;
private LocalDate Dudate;
public Todo() // Def Ctor
{
}
public Todo (String taskstatus,LocalDate dudate,String subject,long customerid){
this.customerID= customerid;
this.Subject = subject;
this.Dudate = dudate;
this.taskstatus = taskstatus;
}
public Todo(long id, long customerid, long descriptionid, String task) { // Ctor all
this.ID=id;
this.customerID=customerid;
this.descriptionID=descriptionid;
this.taskStatus=task;
}
public long getID() {
return ID;
}
public void setID(long ID) {
this.ID = ID;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public long getDescriptionID() {
return descriptionID;
}
public void setDescriptionID(long descriptionID) {
this.descriptionID = descriptionID;
}
public String getTaskStatus() {
return taskStatus;
}
public void setTaskStatus(String taskStatus) {
this.taskStatus = taskStatus;
}
public String getSubject() {
return Subject;
}
public void setSubject(String subject) {
this.Subject = subject;
}
public LocalDate getDudate() {
return Dudate;
}
public void setDudate(LocalDate dudate) {
this.Dudate = dudate;
}
public long getCustomerID() {
return customerID;
}
public void setCustomerID(long customerID) {
this.customerID = customerID;
}
}
TodoView.java
package com.packagename.ui.views;
@Route(value = NAV,layout = MainView.class)
public class TodoView extends Composite<Div> {
public static final String NAV = "todo";
public Todo todo = new Todo();
private Grid<Todo> todogrid = new Grid<>(Todo.class);
private Binder<Todo> binder = new Binder<>(Todo.class);
@Autowired
@SuppressWarnings( "SpringJavaAutowiredMembersInspection")
private TodoService service ;
private Button addButton = new Button("Save", // } catch (ValidationException ex) {
// ex.printStackTrace();
this::onComponentEvent);
public TodoView(){
//Layouts//
HorizontalLayout todofields = new HorizontalLayout();
VerticalLayout todoList = new VerticalLayout();
HorizontalLayout subjects = new HorizontalLayout();
todogrid.addThemeNames("no-border","no-row-borders","row-strips");
GridMultiSelectionModel<Todo> selectionModel = (GridMultiSelectionModel<Todo>) todogrid
.setSelectionMode(Grid.SelectionMode.MULTI);
selectionModel.selectAll();
//Layouts//
//Fields//
ComboBox<String> taskstatus = new ComboBox<>("TaskStatus:");
taskstatus.setItems("Completed","OnProgress","Cancelled");
TextField subject = new TextField();
subject.setLabel("נושא:");
TextField customerid = new TextField();
customerid.setLabel("שם הלקוח:");
DatePicker dudate = new DatePicker();
dudate.setClearButtonVisible(true);
dudate.setLabel("תאריך סיום:");
//Binder//
binder.forField(subject).bind(Todo::getSubject,Todo::setSubject);
binder.forField(dudate).bind(Todo::getDudate,Todo::setDudate);
binder.forField(taskstatus).bind(Todo::getTaskStatus,Todo::setTaskStatus);
binder.forField(customerid).withConverter(new StringToLongConverter(""))
.bind(Todo::getCustomerID,Todo::setCustomerID);
//Binder//
addButton.addClickShortcut(Key.ENTER);
addButton.setThemeName("primary");
//Fields//
todofields.add(subject,customerid,dudate,taskstatus,addButton);
todofields.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.BASELINE);
todoList.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.BASELINE);
todofields.getElement().getStyle().set("margin-left","auto");
getContent().add(todofields,subjects,todoList,todogrid);
Direction.set(Direction.RTL);
}
private void saveTodo() {
service.update(todo);
}
private void updateGrid() {
List<Todo> todo = service.findAll();
todogrid.setItems(todo);
}
private void onComponentEvent(ClickEvent<Button> e) {
try {
binder.writeBean(todo);
System.out.println(todo.getCustomerID());
System.out.println(todo.getTaskStatus());
System.out.println(todo.getDudate());
System.out.println(todo.getSubject());
saveTodo();
updateGrid();
binder.readBean(new Todo());
// } catch (ValidationException ex) {
// ex.printStackTrace();
} catch (ValidationException ex) {
ex.printStackTrace();
}
}
}
我认为问题在于 Route 注释不是由 Spring Boot 管理的。因此,任何@Autowired 都不起作用。
您是否尝试添加 @Spring 组件注释 (https://vaadin.com/api/vaadin-spring/12.0.6/com/vaadin/flow/spring/annotation/SpringComponent.html)?
您的服务可能驻留在未被 spring 扫描的包中,因此 spring 不知道有任何可注入 bean,它没有找到任何。你需要做的是告诉 Spring 在哪里寻找组件,或者将你的 Application
class 一些包向上移动(它会自动扫描它下面的所有包 IIRC)。
您可以在 Application
class 的 @SpringBootApplication
注释中定义 spring 必须在何处查找 spring-组件:提供软件包列表在属性 scanBasePackages
中有 spring 个组件。
一旦 Spring 知道您的 TodoService
,它就会成功地将其注入您的视图。
编辑:SQL 对 TodoService::findAll
的查询似乎也很奇怪,您 select 列 ID, TaskStatus, dudate, subject
但从结果集中您读取了值 ID, UserID, descriptionID, taskstatus
.这不是导致您当前错误的原因,但在您解决当前错误后会导致另一个错误。
我正在尝试 2 天来解决我的问题并了解我错在哪里。 我正在尝试使用 TodoService 在网格上显示项目并将数据添加到我的 SQL。 但我得到错误:
我只是在待办事项视图中的 4 个字段中输入内容,然后单击保存(הוסף) 按钮。 我猜我的服务有问题。但我不明白是什么。 人们试图帮助我,但似乎无济于事。
java.lang.NullPointerException: null
at com.packagename.ui.views.TodoView.saveTodo(TodoView.java:111) ~[classes/:na]
at com.packagename.ui.views.TodoView.onComponentEvent(TodoView.java:127) ~[classes/:na]
at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:205) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:373) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.component.ComponentEventBus.lambda$addDomTrigger$dd1b7957(ComponentEventBus.java:264) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda$fireEvent(ElementListenerMap.java:441) ~[flow-server-2.0.14.jar:2.0.14]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507) ~[na:na]
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:441) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:59) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:64) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:378) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations(ServerRpcHandler.java:359) ~[flow-server-2.0.14.jar:2.0.14]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507) ~[na:na]
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:359) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:301) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:87) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246) ~[flow-server-2.0.14.jar:2.0.14]
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:95) ~[vaadin-spring-12.0.6.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) ~[javax.servlet-api-4.0.1.jar:4.0.1]
at 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]
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665) ~[javax.servlet-api-4.0.1.jar:4.0.1]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) ~[javax.servlet-api-4.0.1.jar:4.0.1]
at 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]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.22.jar:9.0.22]
at org.apache.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]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.apache.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]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.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]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at 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]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at 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.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
TodoService.java
@Component
public class TodoService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Todo> findAll() {
return jdbcTemplate.query(
"SELECT ID, TaskStatus, dudate,subject FROM todo",
(rs, rowNum) -> new Todo(rs.getLong("ID"),
rs.getLong("UserID"), rs.getLong("descriptionID"),rs.getString("taskstatus")));
}
public void update(Todo todo) {
jdbcTemplate.update("INSERT INTO todo (TaskStatus,dudate,subject,CustomerID) VALUES(?,?,?,?)",todo.getTaskStatus(),todo.getDudate(),todo.getSubject(),todo.getCustomerID());
}
}
Todo.java
public class Todo {
private long userID;
private String taskstatus;
private long ID;
private long customerID;
private long descriptionID;
private String taskStatus;
private String Subject;
private LocalDate Dudate;
public Todo() // Def Ctor
{
}
public Todo (String taskstatus,LocalDate dudate,String subject,long customerid){
this.customerID= customerid;
this.Subject = subject;
this.Dudate = dudate;
this.taskstatus = taskstatus;
}
public Todo(long id, long customerid, long descriptionid, String task) { // Ctor all
this.ID=id;
this.customerID=customerid;
this.descriptionID=descriptionid;
this.taskStatus=task;
}
public long getID() {
return ID;
}
public void setID(long ID) {
this.ID = ID;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public long getDescriptionID() {
return descriptionID;
}
public void setDescriptionID(long descriptionID) {
this.descriptionID = descriptionID;
}
public String getTaskStatus() {
return taskStatus;
}
public void setTaskStatus(String taskStatus) {
this.taskStatus = taskStatus;
}
public String getSubject() {
return Subject;
}
public void setSubject(String subject) {
this.Subject = subject;
}
public LocalDate getDudate() {
return Dudate;
}
public void setDudate(LocalDate dudate) {
this.Dudate = dudate;
}
public long getCustomerID() {
return customerID;
}
public void setCustomerID(long customerID) {
this.customerID = customerID;
}
}
TodoView.java
package com.packagename.ui.views;
@Route(value = NAV,layout = MainView.class)
public class TodoView extends Composite<Div> {
public static final String NAV = "todo";
public Todo todo = new Todo();
private Grid<Todo> todogrid = new Grid<>(Todo.class);
private Binder<Todo> binder = new Binder<>(Todo.class);
@Autowired
@SuppressWarnings( "SpringJavaAutowiredMembersInspection")
private TodoService service ;
private Button addButton = new Button("Save", // } catch (ValidationException ex) {
// ex.printStackTrace();
this::onComponentEvent);
public TodoView(){
//Layouts//
HorizontalLayout todofields = new HorizontalLayout();
VerticalLayout todoList = new VerticalLayout();
HorizontalLayout subjects = new HorizontalLayout();
todogrid.addThemeNames("no-border","no-row-borders","row-strips");
GridMultiSelectionModel<Todo> selectionModel = (GridMultiSelectionModel<Todo>) todogrid
.setSelectionMode(Grid.SelectionMode.MULTI);
selectionModel.selectAll();
//Layouts//
//Fields//
ComboBox<String> taskstatus = new ComboBox<>("TaskStatus:");
taskstatus.setItems("Completed","OnProgress","Cancelled");
TextField subject = new TextField();
subject.setLabel("נושא:");
TextField customerid = new TextField();
customerid.setLabel("שם הלקוח:");
DatePicker dudate = new DatePicker();
dudate.setClearButtonVisible(true);
dudate.setLabel("תאריך סיום:");
//Binder//
binder.forField(subject).bind(Todo::getSubject,Todo::setSubject);
binder.forField(dudate).bind(Todo::getDudate,Todo::setDudate);
binder.forField(taskstatus).bind(Todo::getTaskStatus,Todo::setTaskStatus);
binder.forField(customerid).withConverter(new StringToLongConverter(""))
.bind(Todo::getCustomerID,Todo::setCustomerID);
//Binder//
addButton.addClickShortcut(Key.ENTER);
addButton.setThemeName("primary");
//Fields//
todofields.add(subject,customerid,dudate,taskstatus,addButton);
todofields.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.BASELINE);
todoList.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.BASELINE);
todofields.getElement().getStyle().set("margin-left","auto");
getContent().add(todofields,subjects,todoList,todogrid);
Direction.set(Direction.RTL);
}
private void saveTodo() {
service.update(todo);
}
private void updateGrid() {
List<Todo> todo = service.findAll();
todogrid.setItems(todo);
}
private void onComponentEvent(ClickEvent<Button> e) {
try {
binder.writeBean(todo);
System.out.println(todo.getCustomerID());
System.out.println(todo.getTaskStatus());
System.out.println(todo.getDudate());
System.out.println(todo.getSubject());
saveTodo();
updateGrid();
binder.readBean(new Todo());
// } catch (ValidationException ex) {
// ex.printStackTrace();
} catch (ValidationException ex) {
ex.printStackTrace();
}
}
}
我认为问题在于 Route 注释不是由 Spring Boot 管理的。因此,任何@Autowired 都不起作用。 您是否尝试添加 @Spring 组件注释 (https://vaadin.com/api/vaadin-spring/12.0.6/com/vaadin/flow/spring/annotation/SpringComponent.html)?
您的服务可能驻留在未被 spring 扫描的包中,因此 spring 不知道有任何可注入 bean,它没有找到任何。你需要做的是告诉 Spring 在哪里寻找组件,或者将你的 Application
class 一些包向上移动(它会自动扫描它下面的所有包 IIRC)。
您可以在 Application
class 的 @SpringBootApplication
注释中定义 spring 必须在何处查找 spring-组件:提供软件包列表在属性 scanBasePackages
中有 spring 个组件。
一旦 Spring 知道您的 TodoService
,它就会成功地将其注入您的视图。
编辑:SQL 对 TodoService::findAll
的查询似乎也很奇怪,您 select 列 ID, TaskStatus, dudate, subject
但从结果集中您读取了值 ID, UserID, descriptionID, taskstatus
.这不是导致您当前错误的原因,但在您解决当前错误后会导致另一个错误。