Search code examples
springspring-mvcdatasourcejdbctemplate

Spring JDBCTemplate. Null pointer exception


I'm trying to set up a SpringMVC website from scratch, but I've hit a dead end.

I'm using autowiring to instanciate JdbcTemplate with a DataSource, but somehow I'm getting a Null pointer exception. I'd appreciate your help with this.

My AppConfig is the next:

@Configuration
@ComponentScan
public class AppConfig {
    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/onlinelibrary");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    /*Deleted this code, still doesn't work
    @Bean
    public Book Book() {
        return new Book();
    }
    */
}

My Book class is as follows:

@Component
public class Book {
    private JdbcTemplate jdbcTemplate;
    private String title;
    private String author;
    private String isbn;

    public Book() {

    }

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

    public ModelMap getBooks() {
        ModelMap model = new ModelMap();
        String sql = "SELECT * FROM Books";
        model.put("data", jdbcTemplate.queryForList(sql));
        return model;
    }
}

And this is the infamous NullPointer Exception: NullPointerException

Any help would be highly appreciated. I probably forgot to do something, but I can't solve it myself, and I can't find anything on StackOverflow that helps me, either (although I've read many articles by now).

UPDATE WITH MORE DATA:

My project structure is the next:

Project structure

And I'm using the Book object in this controller:

@Controller
public class BookController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getBookData(Book book, ModelMap model) {
        model.put("data", book.getBooks());
        return "BookView";
    }
}

Solution

  • When you have @Component over the class, it means Spring will create a Bean for you provided your component scanner is scanning Book class. You don't need

    @Bean
    public Book Book() {
        return new Book();
    }
    

    It's because of this bean that doesn't have jdbcTemplate injected which is throwing NullPointerException.

    Update:

    Your understanding about spring injection is wrong. I have updated the controller code that should work.

    @Controller
    public class BookController {
    
        @Autowired
        Book book;
    
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String getBookData(ModelMap model) {
            model.put("data", book.getBooks());
            return "BookView";
        }
    }
    

    Update: Component Scan

    @ComponentScan(basePackages = "models")
    public class AppConfig {