I am new in Spring / Spring Boot and try to inject a service to a class. Although I have tried some approaches e.g. @Autowired
, the service instance is always null and throws "NullPointerException" error. I am not sure if I need to register this class and service for DI, but as far as I know it is automatically registered when using @Component
or similar annotation in the class. I think I miss some point, but have not found the problem. Could you pls have a look at the code and let me know where the problem or missing point is?
//@Component // I also tried with this, but employeeService is still null
@Data
@EqualsAndHashCode(callSuper = true)
public class EmployeeRequest extends PageableCriteriaRequest {
@Autowired
public EmployeeService employeeService;
public List<SearchCriteria> getSearchCriteriaList() {
List<int> employees = employeeService.employeeList(); // NullPointerException
//code omitted for brevity
}
}