0

Depending on project's requirement, we want to change from JdbcTemplate into NamedParameterJdbcTemplate in database handling. But I got null point error when I test my coding. I'm sure the error has occurred because of my configuration, but I could not resolve it.

Config

   @Value("${spring.datasource.driver-class-name}") 
   protected String driver;

   @Value("${spring.datasource.url}") 
   protected String url;

   @Value("${spring.datasource.username}")
   protected String user;

   @Value("${spring.datasource.password}") 
   protected String pass;

   @Bean
   public DataSource dataSource(){
          BasicDataSource dataSource = new BasicDataSource();
          dataSource.setDriverClassName(driver);
          dataSource.setUrl(url);
            dataSource.setUsername(user);
            dataSource.setPassword(pass);
            dataSource.setTestOnBorrow(true);
            dataSource.setTestOnReturn(true);
            dataSource.setTestWhileIdle(true);
    return dataSource;

   }

   @Bean
   public JdbcTemplate jdbcTemplate(){
       return new JdbcTemplate(dataSource());
   }

   @Bean
   public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
       return new NamedParameterJdbcTemplate(dataSource);
   }

DaoImpl

@Repository
public class DAOImpl implements DAO {
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public List<Type> findAllType() {
        String sql = "select * from type";
        RowMapper<Type> rm = new TypeRowMapper();
        List<Type> list = jdbcTemplate.query(sql, rm);
        return list;
    }

}

Test

public class Test {
    @Autowired
    protected DAOImpl dao;

    public static void main(String[] args) {
        List<Type> list = new ArrayList<Type>();
        Test test = new Test();
        System.out.println(test.dao);
        list = test.dao.findAllType();
        for(Type type : list){
            System.out.println(type.getName());
        }

    }

}

Stacktrace

null Exception in thread "main" java.lang.NullPointerException at com.example.dao.Test.main(Test.java:18)

Thịnh Kều
  • 163
  • 1
  • 1
  • 17
  • 1
    If you're creating instances manually by using `new` then Spring isn't involved at all. You have to initialize all dependencies in this case. – Slava Semushin Feb 10 '17 at 11:29
  • Which dependencies i need, could you give me an example? – Thịnh Kều Feb 10 '17 at 11:44
  • 1
    How are you injecting the dependency `namedParameterJdbcTemplate()`? Try to apply the breakpoint at `findAllType()` method and check exactly where the exception is thrown. In your `findAllType()` method you are using `namedParameterJdbcTemplate` but you have declared dependency with different name `private NamedParameterJdbcTemplate jdbcTemplate;` – VPK Feb 10 '17 at 11:46
  • When i rename the bean in Config class to jdbcTemplate, i still got nullPoint error bro. – Thịnh Kều Feb 10 '17 at 12:07
  • This isn't about dependencies but about your code. In the example you are not using Spring. – Slava Semushin Feb 10 '17 at 12:14
  • I didn't understand what you mean. But if i block all beans what i declare in config class, then in DaoImpl class i try to autowired as below @Autowired private NamedParameterJdbcTemplate template; It's still not working. – Thịnh Kều Feb 10 '17 at 12:24
  • 1
    You are doing `new Test()` spring doesn't know about the tinstance, nothing will be injected. Also you are using Spring Boot why are you doing all that manual configuration? Spring Boot already configures a `DataSource`, `JdbcTemplate` **and** `NamedParameterJdbcTemplate`. – M. Deinum Feb 10 '17 at 12:41
  • You're right. That is what answer i tried to find. Cause i didn't know exactly does the Spring Boot scan automatically NamedParamaterJdbcTemplate or not, so i try to configure it manually using annotation, thanks. – Thịnh Kều Feb 10 '17 at 15:02
  • So is there any ways autowire it in static class? – Thịnh Kều Feb 10 '17 at 15:17

0 Answers0