0

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
    }
}
  • 1
    you said it yourself, you're missing proper annotation... – Marek Apr 15 '21 at 19:47
  • @Marek Sorry, but I already used `@Component` and it was not visible due to backticks in the question. Now it is displayed. Any idea? –  Apr 15 '21 at 20:15
  • I think the order of the annotations is not important. Isn't it? –  Apr 15 '21 at 20:17
  • @Marek Would you test the code please? –  Apr 15 '21 at 20:41
  • just follow instructions from Akif Hadziabdic, you must properly annotate both EmployeeRequest and EmployeeService classes – Marek Apr 15 '21 at 20:45
  • @Marek I followed and tried all of them + some other combinations, but still the same *"NullPointerException"* problem. Any idea? –  Apr 15 '21 at 22:46
  • The fact that the class is named `EmployeeRequest` it probably means you are using this as an argument in a request handling method. Objects like that don't get injected by Spring. – M. Deinum Apr 16 '21 at 05:44
  • @M.Deinum Yes, the problem is probably caused from that. many thanks... –  Apr 16 '21 at 06:32

2 Answers2

2

You are missing @Component annotation.

@Component
public class EmployeeRequest extends PageableCriteriaRequest {

   @Autowired
   public EmployeeService employeeService;
  ...
}

Also, ensure that EmployeeServiceIml is also annotated with @Component or @Service annotation.

@Service
public class EmployeeServiceIml implements EmployeeService {
  ...
}

Also, ensure that you have @SpringBootApplication and all components in the same or sub-packages.

@SpringBootApplication
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}
Akif Hadziabdic
  • 2,692
  • 1
  • 14
  • 25
  • Thanks Akif, but I already used `@Component` and it was not visible due to backticks in the question. Now it is displayed. Any idea? –  Apr 15 '21 at 20:16
  • Do you have Component or Service on EmployeeService class or implementation class if EmployeeService is an interface? can you share the code? – Akif Hadziabdic Apr 15 '21 at 20:17
  • EmployeeService is an interface and now I am trying to add `@Service` annotation to this interface. Should I add `@Service` annotation to the implementation of the service instead of interface? –  Apr 15 '21 at 20:29
  • Add `@Service` annotation to the implementation, and ensure that you have only one implementation of EmployeeService interface – Akif Hadziabdic Apr 15 '21 at 20:29
  • What about `@Component` and `@Autowired` annotations? Where should I use them for fixing this problem? –  Apr 15 '21 at 20:41
  • `@Component` on class will add that class to spring context and whenever you use `@Autowired `spring context will inject added instance – Akif Hadziabdic Apr 15 '21 at 20:42
  • Could you please update your answer by adding EmployeeRequest, EmployeeService and EmployeeServiceImpl by adding proper annotations? Sorry, I have tried many combinations and I am really very confused as I am beginner for Spring :( –  Apr 15 '21 at 20:49
  • Many thanks for your helps. I have updated all the annotations as you mentioned in your last update. However, still null, I do not know where the problem is. I also build and start debug after each changes so that there is not any problem about that side. –  Apr 15 '21 at 21:19
  • Did you add `@SpringBootApplication` in the class where you have the main method? – Akif Hadziabdic Apr 15 '21 at 21:21
  • Also, check are your components inside sub packages – Akif Hadziabdic Apr 15 '21 at 21:24
  • Do I need to use any annotation e.g. `@RequiredArgsConstructor`? –  Apr 15 '21 at 21:28
  • Yes, `@SpringBootApplication` is added to the main method class. –  Apr 15 '21 at 21:29
  • What do you mean with *"check are your components inside sub packages"* ? –  Apr 15 '21 at 21:29
  • Everything is the same as you suggested, but the service instance is still null :( –  Apr 15 '21 at 22:41
  • can you share full code? – Akif Hadziabdic Apr 16 '21 at 01:56
  • Sorry, but the problem seems to be related to @M.Denium's comment: *"The fact that the class is named EmployeeRequest it probably means you are using this as an argument in a request handling method. Objects like that don't get injected by Spring"*. So, thanks a lot for your help, marked as answer. –  Apr 16 '21 at 06:33
0

You need to put @Service or @Component annotation on the top of EmployeeService class since that's the class' object you want to inject.

@Service
public class EmployeeService {
 // methods of EmployeeService
}
  • Thanks a lot. I also tried it but I think I added unnecessary classes. So, could you please clarify me which annotation should I use in the following classes / services? –  Apr 15 '21 at 20:37
  • The following annotations or need another ones? `@Service`, `@Component` and `@Autowired`. ==> The following classes: `EmployeeRequest`, `EmployeeService` (this is interface) and `EmployeeServiceImpl` (this is service class). –  Apr 15 '21 at 20:39