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