0

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";
    }
}
Chillie
  • 1,030
  • 10
  • 28
IloneSP
  • 429
  • 2
  • 13

1 Answers1

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 {
cosmos
  • 2,143
  • 2
  • 17
  • 27
  • Deleted that code and I'm still getting the same exception :/ – IloneSP Oct 08 '18 at 16:22
  • Then it means, your `Book` class in not being scanned. Can you share your package structure? Also, can you update how are you using Book object and where? – cosmos Oct 08 '18 at 16:25
  • Sure. Updated the main post with your required data. Thanks for the help! – IloneSP Oct 08 '18 at 16:30
  • updated my answer. like I said, i think you misunderstood how to inject dependency. The update should fix it. – cosmos Oct 08 '18 at 16:34
  • Hmm. I understand that I need to inject Book in the controller, then, but using your code IntelliJ prompts me saying "coulnd't autowire, no beans of 'Book' type found". I'm guessing the main issue is that the @Component anotation seems to not be creating a Bean. – IloneSP Oct 08 '18 at 16:40
  • Your package doesn't scan, Book class. – cosmos Oct 08 '18 at 16:41
  • Yes, you either keep, config class in base package common to all classes so that you don't have to define the packages OR define the package like I did in the update. – cosmos Oct 08 '18 at 16:44
  • Now it scans Book class! Thanks a lot!! I'm getting other type of exception, but I'll try to figure it out myself, and if I can't, I guess I'll create a new Question. Thanks a lot again! – IloneSP Oct 08 '18 at 16:52