I am triying to make a simple rest service with Spring and Lombok but I have this problem: "NullPointerException", may you help me please?, belive me, I have tried everithing for insert but always is NullPointerException, here is my source
Entity class Book
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "book")
public class Book{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@NotEmpty
@Column(name = "title")
private String title;
@NotEmpty
@Column(name = "description")
private String description;
}
BookRepository Interface
public interface BookRepository {
List<Book> findAll();
void save(Book book);
Book findOne(int id);
void delate(Book book);
void insert(Book book);
}
BookRepositoyImpl
@Repository
public class BookRepositoryImpl implements BookRepository{
EntityManager em;
BookRepositoryImpl(EntityManager em){
this.em=em;
}
@Override
public List<Book> findAll() {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class);
Root<Book> root =criteriaQuery.from(Book.class);
criteriaQuery
.select(root);
TypedQuery<Book> query= em.createQuery(criteriaQuery);
return query.getResultList();
}
@Override
public void save(Book book) {
em.merge(book);
}
@Override
public Book findOne(int id) {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class);
Root<Book> root =criteriaQuery.from(Book.class);
Predicate IDPredicate =criteriaBuilder.equal(root.get(Book_.ID), id);
criteriaQuery
.select(root)
.where(IDPredicate);
TypedQuery<Book> query= em.createQuery(criteriaQuery);
return query.getSingleResult();
}
@Override
public void delate(Book book) {
em.remove(book);
}
@Override
public void insert(Book book) {
em.persist(book);
}
}
BookService
@Service
@RequiredArgsConstructor
public class BookServices {
private BookRepository bokRepository;
public List <Book> retribeBook(String user){
return bokRepository.findAll();
}
@Transactional
public void DeleteBook(int id) {
Book book=bokRepository.findOne(id);
bokRepository.delate(book);
}
@Transactional
public void UpdateBook(int id, String title) {
Book book= bokRepository.findOne(id);
book.setTitle(title);
bokRepository.save(book);
}
@Transactional
public void insert(Book book) {
bokRepository.insert(book);
}
public Book findId(int id) {
return bokRepository.findOne(id);
}
}
TestController
@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class TestController {
private BookServices bookServices;
@GetMapping
public ResponseEntity<List<Book>> retrieveStudent(@RequestParam final String title) {
return ResponseEntity.ok(bookServices.retribeBook(title));
}
@PutMapping("/{id}")
public ResponseEntity<Void> updateBook(@RequestParam int id, @RequestParam String title ){
bookServices.UpdateBook(id, title);
return ResponseEntity.accepted().build();
}
@PostMapping(consumes =MediaType.APPLICATION_JSON_UTF8_VALUE,path ="/Insert")
public ResponseEntity<Void> Insert(@RequestBody Book book){
bookServices.insert(book);
return ResponseEntity.accepted().build();
}
@DeleteMapping(path = "/{id}")
public ResponseEntity<Void> DeleteBook(@RequestParam int id){
bookServices.DeleteBook(id);
return ResponseEntity.accepted().build();
}
}
When I send a JSON with POSTMAN here is this message on the console
java.lang.NullPointerException: null
at mx.stuteach.controller.TestController.Insert(TestController.java:53) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]