简体   繁体   中英

JdbcTemplate NullPointerException

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.

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

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM