INSERT 语句上的 Spring JDBC NullPointerException [重复]

Posted

技术标签:

【中文标题】INSERT 语句上的 Spring JDBC NullPointerException [重复]【英文标题】:Spring JDBC NullPointerException on INSERT Statement [duplicate] 【发布时间】:2018-11-05 16:11:29 【问题描述】:

我正在处理一个 Maven、Spring-Boot、Vaadin 项目,当我尝试在我的大学表中创建一个新实体时,我无法解决 NullPointerException。

我创建了一个新视图并将所有内容绑定在一起,但如果我想使用示例值执行我的 saveUniversity() 函数,它总是会引发 NullPointerException。希望这是所有相关的代码。

AddUniversityView.java

public class AddUniversityView extends VerticalLayout 

private TextField name = new TextField("Name");
private TextField city = new TextField("City");
private TextField country = new TextField("Country");

private Button save = new Button("Save", e -> saveUniversity()); // Line 23

Binder<University> binder = new Binder<>();

private University university = new University();

@Autowired
private UniversityService universityService;

public AddUniversityView() 

    add(name, city, country, save);



    /**
     * Some values for example
     */
    name.setValue("LMU");
    city.setValue("Muenchen");
    country.setValue("Deutschland"); 


    binder.bind(name, University::getName, University::setName);
    binder.bind(city, University::getCity, University::setCity);
    binder.bind(country, University::getCountry, University::setCountry);


public void saveUniversity() 
    binder.setBean(university);
    System.out.println(binder.getBean().toString());
    universityService.create(university); // Line 109
    clearFields();


public void clearFields() 
    binder.readBean(null);

大学服务

@Service
public class UniversityService 

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void create(University university) 
        jdbcTemplate.update("INSERT INTO university (name, city, country) VALUES (?, ?, ?)", 
                university.getName(), university.getCity(), university.getCountry());
    

application.properties

spring.datasource.url:jdbc:mysql://localhost/univers
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver

大学数据库

+-------------------+
| Tables_in_univers |
+-------------------+
| student           |
| university        |
+-------------------+

大学专栏

+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(45) | NO   |     | NULL    |                |
| city    | varchar(45) | NO   |     | NULL    |                |
| country | varchar(45) | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

错误

java.lang.NullPointerException: null
    at com.sandrofriedrich.ui.views.university.AddUniversityView.saveUniversity(AddUniversityView.java:109) ~[classes/:na]
    at com.sandrofriedrich.ui.views.university.AddUniversityView.lambda$0(AddUniversityView.java:23) ~[classes/:na]
    at com.vaadin.flow.component.ComponentEventBus.fireEvent(ComponentEventBus.java:133) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:327) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.component.ComponentEventBus.lambda$addDomTrigger$5ee67f2b$1(ComponentEventBus.java:191) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda$fireEvent$2(ElementListenerMap.java:379) ~[flow-server-1.0.5.jar:na]
    at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_181]
    at com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:379) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:59) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:64) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:377) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations$0(ServerRpcHandler.java:367) ~[flow-server-1.0.5.jar:na]
    at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_181]
    at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:367) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:309) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:89) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1486) ~[flow-server-1.0.5.jar:na]
    at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:300) [flow-server-1.0.5.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:470) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:356) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) [spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-embed-core-8.5.34.jar:8.5.34]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.34.jar:8.5.34]
    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.34.jar:8.5.34]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]

【问题讨论】:

假设 universityService.create(university); 确实是第 109 行,您是否尝试运行调试器并检查 universityService 是否为 null,如果是,为什么? 【参考方案1】:

如何创建AddUniversityView

如果它与@Route 注释一起使用,或者如果它也在其他类中自动装配,它应该可以工作。

但是,如果您自己实例化它,如new AddUniversityView(),自动装配将不起作用。

【讨论】:

谢谢你为我工作【参考方案2】:

@Autowired 表示,外部人员将设置此字段。另一方面,“私有”意味着除了这个类之外没有人可以使用它。

你应该像这样改变你的代码

@Autowired
UniversityService universityService;

希望对您有所帮助!

【讨论】:

Spring 完全能够自动装配private 字段。

以上是关于INSERT 语句上的 Spring JDBC NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章

使用 Postgresql JDBC 时,将导致插入 0 行的 INSERT 语句

JDBC-根据ResultSet返回值生成Insert-Sql语句

如何得到JDBC Insert 语句执行后插入Oracle 数据库记录的主键

JDBC Insert语句插入Oracle数据库返回数据主键

为啥必须将表中的至少一列(在 MySQL 中)分配为 PRIMARY KEY,以便表通过 JDBC 接受 UPDATE 和 INSERT 语句?

java下连接mysql数据库 ——插入语句——insert