0

I'm getting NullPointerException in the bellow code at controller itself. Before going to service class. I couldn't find where I go wrong. As I'm new to spring.

java.lang.NullPointerException: Cannot invoke "com.sarathUniversity.service.StudentService.createStudentResponse(com.sarathUniversity.request.CreateStudentRequest)" because "this.stdService" is null

2023-05-30T13:13:08.197+05:30  INFO 11192 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-30T13:13:08.197+05:30  INFO 11192 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-05-30T13:13:08.198+05:30  INFO 11192 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2023-05-30T13:22:44.633+05:30  WARN 11192 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=9m44s455ms305µs600ns).
2023-05-30T13:22:55.734+05:30 ERROR 11192 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "com.sarathUniversity.service.StudentService.createStudentResponse(com.sarathUniversity.request.CreateStudentRequest)" because "this.stdService" is null] with root cause

java.lang.NullPointerException: Cannot invoke "com.sarathUniversity.service.StudentService.createStudentResponse(com.sarathUniversity.request.CreateStudentRequest)" because "this.stdService" is null
    at com.sarathUniversity.controller.StudentController.createStudent(StudentController.java:17) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:152) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.9.jar:6.0.9]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.9.jar:6.0.9]

In the bellow picture you can see the debug values. In that the

StudentRepository = null
AdressRepository = null

enter image description here

enter image description here

Passing the body content in post method and also I can see those in debug console.

http://localhost:8080/api/student/create

{
  "firstName":"sarathdaa",
  "lastName": "Kumarda",
  "email": "silver@sarath.com",
  "street": "Pall,Tvk,256",
  "city": "chennai"
}

UPDATE Tried @Autowired over StudentService @Service over StudentService class enter image description here

enter image description here

Shared the source link bellow.

https://github.com/sarath14kumar/SpringBootMicroService/blob/master/src/main/java/com/sarathUniversity/controller/StudentController.java#L17

Please help me to rectify this. Thanks in advance. If more detail needed, please comment bellow.

S14321K
  • 220
  • 3
  • 19

2 Answers2

2

You are not using Dependency Injection (DI). There are several ways to do this injection in Spring architecture.

@RestController
@RequestMapping("/api/student")
public class StudentController extends CreateStudentRequest
{
    private final StudentService stdService;

    // a constructor so that the Spring container can inject a StudentService.
    public StudentController(StudentService stdService) {
        this.stdService = stdService;
    }

    // ...
}

Additionally,

I forked your code and here is the result:

enter image description here

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
  • Sorry this didn't work. Server stopped with error. – S14321K May 30 '23 at 10:01
  • `The dependencies of some of the beans in the application context form a cycle: ┌──->──┐ | studentController defined in file [E:\IntellijProjects\UniversityMonolithicApp\target\classes\com\sarathUniversity\controller\StudentController.class] └──<-──┘ Action: Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.` – S14321K May 30 '23 at 10:02
  • @Sarath14Kumar it did work, but now you get another error, and the error message explains what's wrong. You have a cyclic dependency, which you need to fix. Does `StudentService` also have the controller as a dependency? It shouldn't have. – Jesper May 30 '23 at 10:17
  • Yes, this worked for me. Now facing same problem in StudentService class. `java.lang.NullPointerException: Cannot invoke "com.sarathUniversity.repository.AdressRepository.save(Object)" because "this.adressRepository"` Should I do the same here? – S14321K May 30 '23 at 10:31
  • `addrs = adressRepository.save(addrs);` in this line – S14321K May 30 '23 at 10:31
  • Why should I do this way when @AutoWired is available. May I know – S14321K May 30 '23 at 10:35
  • See https://stackoverflow.com/q/40620000/2039546 – İsmail Y. May 30 '23 at 11:16
1

There may be two issues here

  1. you need to annotate StudentService with @Service annotation
  2. you need to annotate stdService object with @Autowired annotation. First try this and then only we can debug further
  • Tried with your opinion, but didn't work. Updated the question now with your. – S14321K May 30 '23 at 10:12
  • you are extending CreateStudentRequest from StudentService and also extending StudentController from CreateStudentRequest and then Auto wiring StudentService. it does not work like that spring boot I don't know why you are extending CreateStudentRequest from StudentService, also you need to Annotate your repository with @Repository and auto-wire them in service. – Nandish Chauhan May 30 '23 at 10:27