Java JdbcTemplate 空指针异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23198660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:17:07  来源:igfitidea点击:

JdbcTemplate NullPointerException

javaspringjdbctemplate

提问by Tom Joad

I am trying to save data to database with Spring's JdbcTemplate, but I get this error message. If I do it normal way with PreparedStatements it's working.

我正在尝试使用 Spring 的 JdbcTemplate 将数据保存到数据库,但收到此错误消息。如果我使用 PreparedStatements 以正常方式进行操作,则它可以正常工作。

https://i.imgur.com/OpBpTE8.png

https://i.imgur.com/OpBpTE8.png

My CarDAO class:

我的 CarDAO 类:

@Repository
@Service
public class CarDAO implements CarDAOService {

private JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}



public void saveCarToDB(CarBean carbean) {

    final String sql = "insert into car (make, model) values (?,?)";

    Object[] parameters = new Object[] {carbean.getMake()+
            carbean.getModel()};

    //if I do here system.out.print(Arrays.toString(parameters)); 
    //it will print right make/model.

    jdbcTemplate.update(sql, parameters);

   //console says it is that row above, but I don't get how. Both parameters has values?
   //WARNING: StandardWrapperValve[spring-dispatcher]: Servlet.service() for servlet 
   //springdispatcher threw exception

}

Spring-base.xml

Spring-base.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="bean, dao" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/Jsp/" />
    <property name="suffix" value=".jsp" />
</bean>


<!-- DATA SOURCE -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="org.mariadb.jdbc.Driver" />
    <property name="url" value="jdbc:mariadb://XXXXX" />
    <property name="username" value="XXXXX" />
    <property name="password" value="XXXXX" />
    <property name="initialSize" value="1" />
    <property name="maxActive" value="5" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<mvc:annotation-driven />

</beans>

I didn't include my Controller or CarDAOService class, because I think the problem isn't there. They are forwarding right parameters to CarDAO class.

我没有包含我的 Controller 或 CarDAOService 类,因为我认为问题不存在。他们将正确的参数转发给 CarDAO 类。

采纳答案by GriffeyDog

You need to put the @Autowiredannotation on your setter:

您需要将@Autowired注释放在您的 setter 上:

@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

回答by Sireesh Yarlagadda

Autowired bean via @Autowired, and it can be applied on setter method, constructor or a field.

通过@Autowired 自动装配 bean,它可以应用于 setter 方法、构造函数或字段。

Try this

尝试这个

@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}