You cannot instantiate the ClientService yourself, using new ClientService()! If you do that then the clientRepository will not be autowired and therefore be null at the time of calling getAll().
You need to autowire the ClientService into your view! Never instantiate a Spring-Component (classes annotated with @Service or @Controller or @Repository - which are all extentions of @Component) yourself.
Edit: wait I was confused by your code markdown i didnt see @route.
You use field injection instead of constructor injection. When doing field injection, the clientService will be autowired AFTER the constructor. But you want to use it within the constructor
@Route
public class ProductForm extends VerticalLayout {
@Autowired
public ProductForm(ClientService clientService) {
// clientService is now not null in the constructor!
clientService.getAll(); // works
}
}
@Route
public class ProductForm extends VerticalLayout {
@Autowired ClientService clientService
public ProductForm() {
// clientService is null here
}
@PostConstruct
public postConstructor(){
// clientService is not null here
clientService.getAll(); // works
}
}
Edit: wait I was confused by your code markdown i didnt see @route.
You use field injection instead of constructor injection. When doing field injection, the clientService will be autowired AFTER the constructor. But you want to use it within the constructor
@Route
public class ProductForm extends VerticalLayout {
// this is called constructor injection.
// You don't even need the @Autowired annotation but I like it because it shows me that some autowiring is happening
@Autowired
public ProductForm(ClientService clientService) {
// clientService is now not null in the constructor!
clientService.getAll(); // works
}
}
@Route
public class ProductForm extends VerticalLayout {
@Autowired ClientService clientService
public ProductForm() {
// clientService is null here
}
@PostConstruct
public postConstructor(){
// clientService is not null here
clientService.getAll(); // works
}
}