by @kodeazy
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception NullPointerException
I was trying to create a sample CRUD
operation using spring boot and insert
the data into h2
data base.
Got the Below error.
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "com.example.demo.service.BookService.insertData(com.example.demo.model.Book)" because "this.bserv" is null] with root cause
java.lang.NullPointerException: Cannot invoke "com.example.demo.service.BookService.insertData(com.example.demo.model.Book)" because "this.bserv" is null
at com.example.demo.controller.BookController.insertData(BookController.java:24) ~[main/: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]
Below are my model
,controller
,service
class files and application.properties
file
Book.java
package com.example.demo.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
public String name;
public String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
BookController.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
@RestController
public class BookController {
BookService bserv;
@PostMapping("/insert")
public void insertData(@RequestBody Book b1) {
bserv.insertData(b1);
}
}
BookService.java
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.Book;
import com.example.demo.repo.BookRepo;
@Service
public class BookService {
@Autowired
BookRepo repo;
public void insertData(Book b1) {
// TODO Auto-generated method stub
repo.save(b1);
}
}
application.properties
pring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:yourDB
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
To resolve the error follow below steps
In the error it specifies that this.bserv is null at com.example.demo.controller.BookController
.
That means bserv
variable is not initialized in BookController.java
file.
To initialize the variable we have to autowire
in springboot using @Autowired
annotation.
After modification below is my BookController.java
file
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
@RestController
public class BookController {
@Autowired// Here we added the autowire
BookService bserv;
@PostMapping("/insert")
public void insertData(@RequestBody Book b1) {
bserv.insertData(b1);
}
}
Now try running the application.