Reputation: 13
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;
}
}
Upvotes: 1
Views: 8212
Reputation: 357
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.
Upvotes: 1
Reputation: 2711
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
Upvotes: 0
Reputation: 1261
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.
Upvotes: 4