I'm new to Java and Spring. My problem is that using the autowired field in some of my controller functions throws NullPointerException.
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
@GetMapping("/list")
public String listCustomers(Model model) {
List<Customer> customers = customerService.getCustomers();
model.addAttribute("customers", customers);
return "list-customer";
}
@PostMapping("/save")
private String save(@ModelAttribute("customer") Customer customer) {
customerService.saveCustomer(customer);
return "redirect:/customer/list";
}
@GetMapping("/delete")
private String delete(@RequestParam("customerId") int id, Model model) {
customerService.removeCustomer(customerService.getCustomer(id));
return "redirect:/customer/list";
}
}
This happens in the save
or delete
function but not in the list
.
java.lang.NullPointerException: Cannot invoke "com.hamid.springdemo.service.CustomerService.getCustomer(int)" because "this.customerService" is null
I should mention that I recently added AOP support to my project, but I have no idea whether it is related or not.