JdbcTemplate NullPointerException

泪湿孤枕 提交于 2021-01-05 06:47:45

问题


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.

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

My CarDAO class:

@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

<?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.


回答1:


You need to put the @Autowired annotation on your setter:

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



回答2:


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

Try this

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


来源:https://stackoverflow.com/questions/23198660/jdbctemplate-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!