Spring JDBCTemplate. Null pointer exception

◇◆丶佛笑我妖孽 提交于 2019-12-02 18:36:54

问题


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:

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:

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

回答1:


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 {


来源:https://stackoverflow.com/questions/52706173/spring-jdbctemplate-null-pointer-exception

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