1
votes

I'm a little new to spring and I've been running into a Null Pointer Exception. I believe the @Autowired is not working on my MongoRepository. For some reason when I tried some examples it was working. (The commented out code in the run function worked)

This is the error I get:

2016-05-20 02:31:20.877 ERROR 6272 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null at com.applesauce.service.CustomerService.addCustomer(CustomerService.java:24) ~[classes/:na]

Can you guys take a look and direct me? Also, please let me know if i'm doing something wrong for best practices. If you need anymore information, please ask!

com.applesauce.controller

@RestController 
@RequestMapping("/customer")
public class CustomerController {

private CustomerService customerService = new CustomerService();

@RequestMapping(value = "/addcustomer", method = RequestMethod.GET)
public Customer addCustomer(@RequestParam("firstName") String fName,
                         @RequestParam("lastName") String lName,
                         @RequestParam("email") String email,
                         @RequestParam("phoneNumber") String phoneNumber,
                         @RequestParam("source") String source){
    return customerService.addCustomer(new Customer(fName,lName,email,phoneNumber,source));
}
}

com.applesauce.repository

@Repository
public interface CustomerRepository extends MongoRepository<Customer, String> {

public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}

com.applesauce.service

@EnableMongoRepositories(basePackages = "com.applesauce.repository")
public class CustomerService {

@Autowired
private CustomerRepository repository;

public Customer addCustomer(Customer customer){

    repository.save(customer);

    return customer;
}
}
3
Isn't your CustomerService intended to be a Spring @Service? Annotate as it instead and don't create it manually, use @Autowired. By the way, you should remove all the non-related code from your question, that also includes all the lines being commented.Xtreme Biker
Thanks for the tip. I removed some of the code. Can you explain "Annotate as it instead and don't create it manually, use @Autowired." this part a little more? I'm not understanding.Jackson Chang

3 Answers

4
votes

Xtreme Biker meant, that you should add @Service annotation for your CustomerService, like this:

@EnableMongoRepositories(basePackages = "com.applesauce.repository")
@Service
public class CustomerService {
...
}

Also, you never want to create service using new operator, if you expect Spring to take care about it. In your CustomerController, change initialization line:

private CustomerService customerService = new CustomerService();

to:

@Autowired
private CustomerService customerService;

It must solve NullPointerException.

1
votes

Check you would have created an object of the class which has a @Service annotation (using new). This class should also be autowired instead, where you have autowired your repository bean.

0
votes

I had the same issue and I solved this way (explanation is generic)

Repository.class

@Repository
public interface {...}

Service.class

public class {...}
@Autowired 
private Repository repo;

Application.class

@Autowired Service service;
service.method() //does not throws NullPointerException