【问题标题】:Spring boot throwing null pointer exception while using Spring data JPASpring Boot在使用Spring数据JPA时抛出空指针异常
【发布时间】:2021-08-31 14:04:41
【问题描述】:

我是 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";
    }

}

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

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> {
}

【问题讨论】:

    标签: java spring spring-boot hibernate spring-mvc


    【解决方案1】:

    这显然是由于你的事实

    postRepository 未自动连接。 Spring 无法自行实例化它,除非您使用注释 @Autowired 指定它,或者以 @PostConstruct 的方式亲自实例化它。

    有一个新界面

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

    在你的服务类中。

    @AutoWired
    private PostRepository postRepository;
    

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

    希望对你有帮助!!

    【讨论】:

    • 确保您的包装结构正确。
    【解决方案2】:

    您尚未在 PostService 中初始化 postRepository。 你的意思是让它自动连线吗?

    【讨论】:

    • 是的,它应该是自动装配的。但是在使用@Autowired 之后,它说''考虑在你的配置中定义一个'com.project.newsblog.repository.PostRepository' 类型的bean。''这次是什么问题,先生。 PostRepository 是扩展 JpaRepository 的简单接口。
    【解决方案3】:

    我猜,您错过了将 @Autowired 注释为服务类中的注入 bean

    @自动连线 私有 PostRepository postRepository;

    【讨论】:

    • 是的,这是肯定的,但是当我注释它时,它给出了另一个错误,即。 No qualifying bean of type 'com.project.newsblog.repository.PostRepository。为了更清楚,我已经编辑了这个问题。
    • 你的repository类是存在同一个包还是不同包?
    • 如果它在不同的包中,那么你必须在主 springbootapplication 类中声明 @ComponentScan(basePackages = "com.project.newsblog.*")
    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多