0

this is a part of TopicService.java

I get a NULL pointer exception at this line TopicRepo.findAll().forEach(topics::add);

private topicRepo TopicRepo; this is where I am auto wiring the dependency

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);

    }

topicRepo.java, this is where I am extending the crud repository

package io.javabrains.example.topic;

import org.springframework.data.repository.CrudRepository;

public interface topicRepo extends CrudRepository<Topic, String> {

}

and this is what is have in my application.properties file

#JPA
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true

here is a part of my 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() {

    }
Nick
  • 7
  • 1
  • 4

3 Answers3

0

Is Topic class annotated with JPA @Entity and have @Id on String field ? Note: Try to declare Class/Interface names in Capitalized format.

-1

Add @Repository annotation in your topicRepo interface :

@Repository
 public interface topicRepo extends CrudRepository<Topic, String> {
anakin59490
  • 630
  • 1
  • 11
  • 28
  • Still giving the same error when I send a get request, It works fine if I do not use crud operations from the repository though. – Nick May 27 '21 at 18:09
  • ok try to extend JpaRepository instead of CrudRepository – anakin59490 May 27 '21 at 18:52
  • still the same, not sure if I'm making a really stupid mistake – Nick May 27 '21 at 19:54
  • Adding `@Repository` to an interface adds absolutely nothing. – M. Deinum May 28 '21 at 05:41
  • @M.Deinum if I remove Repository annotaion from my project it doesn't work so tell us why it adds absolutely nothing instead of mark my answer as not useful ! – anakin59490 May 28 '21 at 07:36
  • Because Spring Data doesn't use the `@Repository` annotation to detect repositories. It uses different means, so adding that annotation to a Spring Data interface driven repository adds nothing. Interfaces are even ignored when the component-scanning detects them as you cannot create an instance of an interface. – M. Deinum May 28 '21 at 07:44
-1

If I remember well you still have to create the functions/methods in the @Repository file that you need as below example from docs. Also try to keep the syntax for classes to start with UpperCase letter TopicRepo instead of topicRepo and objects with lower case topicReport instead of TopicRepo:

@Autowired
TopicRepo topicReport

and finally your repo interface:

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

  List<Topic> findAll();

}
Ferlorin
  • 50
  • 3
  • I'm pretty sure since the findAll() method comes from the Crud repo, the creation isn't necessary. But I tried both still giving the same Null pointer exception – Nick May 27 '21 at 19:52
  • `findAll` is part of `CrudRepository` 's default methods, so no you don't need to add them. – M. Deinum May 28 '21 at 05:41