首页 > 解决方案 > Spring Boot在使用Spring数据JPA时抛出空指针异常

问题描述

我是 spring 的新手,当我尝试将实体保存到数据库时,它会引发空指针异常。以下是相关代码供参考:-

这是控制器:-

import com.project.newsblog.entities.Post;
import com.project.newsblog.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired
    public PostService postService;


    @PostMapping(value = "/post")
    public String post(@RequestBody Post item){
        System.out.println(item.toString());
        postService.post(item);
        return "Successful";
    }

}

这是服务服务(错误的根本原因):-

import com.project.newsblog.entities.Post;
import com.project.newsblog.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostService {

    private PostRepository postRepository;

    public List<Post> getAllPost(){
        return postRepository.findAll();
    }
    public void post(Post post){
        postRepository.save(post);
    }
}

这是供参考的实体:-

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;

@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String creator;
    private  String title;
    private String text;
    private LocalDateTime dateTime;

    public Post() {
    }

    @Override
    public String toString() {
        return "Post{" +
                "id=" + id +
                ", creator='" + creator + '\'' +
                ", title='" + title + '\'' +
                ", text='" + text + '\'' +
                ", dateTime=" + dateTime +
                '}';
    }

    public Post(String creator, String title, String text) {
        this.creator = creator;
        this.title = title;
        this.text = text;
        this.dateTime = LocalDateTime.now();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }
}

将 json 有效负载保存到数据库时似乎存在问题。我想我错过了一些我无法弄清楚的东西。请帮帮我。这是最相关的调试消息:-

Post{id=1, creator='Someone', title='This is the title', text='This is the title', dateTime=null}
2021-06-15 22:52:22.145 DEBUG 14756 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet        : Failed to complete request: java.lang.NullPointerException
2021-06-15 22:52:22.146 ERROR 14756 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.project.newsblog.service.PostService.post(PostService.java:20) ~[classes/:na]
    at com.project.newsblog.controller.MyController.post(MyController.java:21) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]

编辑:- 当我使用@Autowired 在服务部分中注释 PostRepository 行时,它给出了错误:“没有 'com.project.newsblog.repository.PostRepository' 类型的合格 bean”。这是存储库代码:-

package com.project.newsblog.repository;

import com.project.newsblog.entities.Post;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
}

标签: javaspringspring-boothibernatespring-mvc

解决方案


这显然是由于您的

postRepository不是自动接线的。在您使用 annotation 指定它之前,Spring 无法自行@Autowired实例化它,或者以某种方式亲自实例化它@PostConstruct

有一个新的界面

public interface PostRepository extends JpaRepository<Post , Long>{

}

在你的服务班上。

@AutoWired
private PostRepository postRepository;

有这样的包裹。所有的包都应该在 SpringBootApp 包的目录下。

在此处输入图像描述

希望这可以帮助 !!


推荐阅读