Java 我试图使用spring boot执行一个简单的crud操作findall(),它符合要求,没有任何错误或警告,但给出一个NULL异常

Java 我试图使用spring boot执行一个简单的crud操作findall(),它符合要求,没有任何错误或警告,但给出一个NULL异常,java,spring,spring-boot,spring-mvc,crud-repository,Java,Spring,Spring Boot,Spring Mvc,Crud Repository,这是TopicService.java的一部分 我在这一行得到一个空指针异常 TopicRepo.findAll().forEach(主题::添加) 私人topicRepo topicRepo;这就是我自动连接依赖项的地方 package io.javabrains.example.topic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype

这是TopicService.java的一部分

我在这一行得到一个空指针异常 TopicRepo.findAll().forEach(主题::添加)

私人topicRepo topicRepo;这就是我自动连接依赖项的地方

package io.javabrains.example.topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class TopicService {// creating a business service

    @Autowired
    private topicRepo TopicRepo; // whenever the TopicService creates an instance then the instance of topicRepo will be injectjed in the variable

    private ArrayList<Topic> topics = new ArrayList<>(Arrays.asList(
                new Topic("spring1", "nishhcal", "name"),
                new Topic("spring2", "nishhca2", "name2"),
                new Topic("spring3", "nishhca3", "name3")));



    public List<Topic> getAllTopics(){

//        return topics;
        List<Topic> topics = new ArrayList<>();
        TopicRepo.findAll().forEach(topics::add);
        return topics;
    }

    public Topic getSpecificTopic(String id){
//        for (int r=0; r < topics.size(); r++){
//
//            if((topics.get(r).getId()).equals(id)){
//
//                return topics.get(r);
//            }
//        }
//        return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();// alternative way of doing it
        return topics.get(0);
    }

    public void addTopic(Topic topic){
//        topics.add(topic);
        TopicRepo.save(topic);

    }
这是我的Topic.java的一部分

package io.javabrains.example.topic;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Topic {

    @Id
    private String id;
    private String name;
    private String description;


    public Topic() {

    }

主题类是否用JPA@Entity注释并在字符串字段上有@Id?
注意:尝试以大写格式声明类/接口名称。

在topicRepo接口中添加@Repository注释:

@Repository
 public interface topicRepo extends CrudRepository<Topic, String> {
@存储库
公共接口topicRepo扩展了crudepository{

如果我记得很清楚,您仍然需要在@Repository文件中创建所需的函数/方法,如下面的示例所示。同时,请尝试保留以大写字母TopicRepo而不是TopicRepo开头的类和以小写字母topicReport而不是TopicRepo开头的对象的语法:

@Autowired
TopicRepo topicReport
最后是您的回购界面:

@Repository
public interface TopicRepo extends CrudRepository<Topic, Long> {

  List<Topic> findAll();

}
@存储库
公共接口TopicRepo扩展了crudepository{
列出findAll();
}

我已经在上面添加了Topic.java,仍然会收到相同的错误在发送get请求时仍然会出现相同的错误,但是如果我不使用存储库中的crud操作,效果会很好。好吧,尝试扩展JpaRepository而不是CRUDepository仍然一样,不确定我是否犯了一个非常愚蠢的错误添加
@repository
如果我从我的项目中删除了存储库注释,它就不起作用了,那么告诉我们为什么它会添加任何内容,而不是将我的答案标记为无用!我很确定,因为findAll()是方法来自Crud repo,不需要创建。但我尝试了这两种方法,但仍然提供相同的空指针异常
findAll
crudepository
的默认方法的一部分,因此不需要添加它们。我猜您正在执行
new TopicService()
,并且仍然希望它是自动连接的。
@Autowired
TopicRepo topicReport
@Repository
public interface TopicRepo extends CrudRepository<Topic, Long> {

  List<Topic> findAll();

}