如何从 Sql 插入和获取数据
How to Insert and get data from Sql
我正在尝试将我的 Todo 表单添加到 sql,但我收到一个空错误,
我用打印输出进行了测试,值在那里。
我的更新网格也没有在网格中显示任何内容我在服务中测试了命令并且似乎显示但网格没有显示我。
你能帮我吗?谢谢你。
我的代码是:
Todo.java
package com.packagename.myapp.spring;
import java.time.LocalDate;
import java.util.Date;
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
@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);
private TodoService service ;
private Button addButton = new Button("Save", 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();
}
});
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);
}
}
mainview.java
package com.packagename.myapp.spring;
import static com.vaadin.flow.component.icon.VaadinIcon.*;
import java.util.HashMap;
import java.util.Map;
@Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
@Theme(Lumo.class)
@Route
@PWA(name = "SimpleIT", shortName = "SimpeIT")
public class MainView extends AppLayout {
public static final String ITM_DASHBOARD = "DashBoard";
private Map<Tab, Component> tab2Workspace = new HashMap<>();
public MainView() {
Direction.set(Direction.RTL);
// setPrimarySection(Section.DRAWER);
Image img = new Image("https://i.ibb.co/KbZbtWm/SimpleIt.png", "Vaadin Logo");
img.setHeight("75px");
addToNavbar(new MenuBar(), img);
Tabs menu = new Tabs(dashBoard()
,customers(),Todo(),Tickets());
menu.setOrientation(Tabs.Orientation.HORIZONTAL);
menu.addSelectedChangeListener(event -> {
final Tab selectedTab = event.getSelectedTab();
final Component component = tab2Workspace.get(selectedTab);
setContent(component);
});
addToNavbar(menu);
this.setPrimarySection(Section.NAVBAR);
setContent(new Span("click in the menu ;-) , you will see me never again.."));
}
///**// Tabs Area//**///
private Tab Tickets() {
final Span label = new Span("קריאות");
final Icon icon = BOOK.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new TicketsView());
return tab;
}
private Tab customers() {
final Span label = new Span("לקוחות");
final Icon icon = BOOK.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new CustomersView());
return tab;
}
private Tab dashBoard() {
final Span label = new Span("ראשי");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new DashBoardView());
return tab;
}
private Tab Todo() {
final Span label = new Span("משימות");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab,new TodoView());
return tab;
}
//**//Ends Of Tab Area//**//
}
TodoService.java
package com.packagename.myapp.spring.Services;
import com.packagename.myapp.spring.Todo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class TodoService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Todo> findAll() {
return jdbcTemplate.query(
"SELECT ID, UserID, notesID,TaskStatus 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());
}
}
错误信息:
java.lang.NullPointerException
at com.packagename.ui.views.TodoView.saveTodo(TodoView.java:119)
at com.packagename.ui.views.TodoView.lambda$new9424d1(TodoView.java:52)
at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:205)
at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:373)
at com.vaadin.flow.component.ComponentEventBus.lambda$addDomTrigger$dd1b7957(ComponentEventBus.java:264)
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda$fireEvent(ElementListenerMap.java:441)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:441)
at com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:59)
at com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:64)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:378)
at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations(ServerRpcHandler.java:359)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:359)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:301)
at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:87)
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540)
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246)
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
正如在评论中已经注意到的那样,您没有注入也没有初始化您的服务:
这是您在 TodoView.java
中唯一的线路,这里的服务为空
private TodoService service ;
这就是您在
获得 NPE 的原因
private void saveTodo() {
service.update(todo);
}
因为您是在 null 上调用方法
所以一个快速的解决方案应该是添加一个
@Autowired
private TodoService service ;
因为您已经在使用 spring
本文中的类似示例:Spring @Autowired tutorial
我正在尝试将我的 Todo 表单添加到 sql,但我收到一个空错误, 我用打印输出进行了测试,值在那里。 我的更新网格也没有在网格中显示任何内容我在服务中测试了命令并且似乎显示但网格没有显示我。 你能帮我吗?谢谢你。 我的代码是: Todo.java
package com.packagename.myapp.spring;
import java.time.LocalDate;
import java.util.Date;
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
@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);
private TodoService service ;
private Button addButton = new Button("Save", 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();
}
});
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);
}
}
mainview.java
package com.packagename.myapp.spring;
import static com.vaadin.flow.component.icon.VaadinIcon.*;
import java.util.HashMap;
import java.util.Map;
@Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
@Theme(Lumo.class)
@Route
@PWA(name = "SimpleIT", shortName = "SimpeIT")
public class MainView extends AppLayout {
public static final String ITM_DASHBOARD = "DashBoard";
private Map<Tab, Component> tab2Workspace = new HashMap<>();
public MainView() {
Direction.set(Direction.RTL);
// setPrimarySection(Section.DRAWER);
Image img = new Image("https://i.ibb.co/KbZbtWm/SimpleIt.png", "Vaadin Logo");
img.setHeight("75px");
addToNavbar(new MenuBar(), img);
Tabs menu = new Tabs(dashBoard()
,customers(),Todo(),Tickets());
menu.setOrientation(Tabs.Orientation.HORIZONTAL);
menu.addSelectedChangeListener(event -> {
final Tab selectedTab = event.getSelectedTab();
final Component component = tab2Workspace.get(selectedTab);
setContent(component);
});
addToNavbar(menu);
this.setPrimarySection(Section.NAVBAR);
setContent(new Span("click in the menu ;-) , you will see me never again.."));
}
///**// Tabs Area//**///
private Tab Tickets() {
final Span label = new Span("קריאות");
final Icon icon = BOOK.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new TicketsView());
return tab;
}
private Tab customers() {
final Span label = new Span("לקוחות");
final Icon icon = BOOK.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new CustomersView());
return tab;
}
private Tab dashBoard() {
final Span label = new Span("ראשי");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new DashBoardView());
return tab;
}
private Tab Todo() {
final Span label = new Span("משימות");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab,new TodoView());
return tab;
}
//**//Ends Of Tab Area//**//
}
TodoService.java
package com.packagename.myapp.spring.Services;
import com.packagename.myapp.spring.Todo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class TodoService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Todo> findAll() {
return jdbcTemplate.query(
"SELECT ID, UserID, notesID,TaskStatus 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());
}
}
错误信息:
java.lang.NullPointerException
at com.packagename.ui.views.TodoView.saveTodo(TodoView.java:119)
at com.packagename.ui.views.TodoView.lambda$new9424d1(TodoView.java:52)
at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:205)
at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:373)
at com.vaadin.flow.component.ComponentEventBus.lambda$addDomTrigger$dd1b7957(ComponentEventBus.java:264)
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda$fireEvent(ElementListenerMap.java:441)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:441)
at com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:59)
at com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:64)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:378)
at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations(ServerRpcHandler.java:359)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:359)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:301)
at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:87)
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540)
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246)
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
正如在评论中已经注意到的那样,您没有注入也没有初始化您的服务:
这是您在 TodoView.java
中唯一的线路,这里的服务为空
private TodoService service ;
这就是您在
获得 NPE 的原因private void saveTodo() {
service.update(todo);
}
因为您是在 null 上调用方法
所以一个快速的解决方案应该是添加一个
@Autowired
private TodoService service ;
因为您已经在使用 spring
本文中的类似示例:Spring @Autowired tutorial