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
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": "[email protected]",
"street": "Pall,Tvk,256",
"city": "chennai"
}
UPDATE Tried @Autowired over StudentService @Service over StudentService class
Shared the source link bellow.
Please help me to rectify this. Thanks in advance. If more detail needed, please comment bellow.
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,
extends CreateStudentRequest
in your StudentController class.@Service
annotation to your StudentService class.I forked your code and here is the result: