0

I implemented a repository, an entity and a POJO where I want to call the findAll() method from the repository, but it returns null pointer exception, even though I have the database populated. The problem is that the repo is null, not the list after executing the select. Can anyone help me?

This is my repo:

 @Repository
 public interface UpcomingMatchesFactsRepository  extends 
 CrudRepository<UpcomingMatchesFacts, Integer> {

    public boolean existsByFirstPlayerName(String firstPlayerName);

    @Query(value = "" +
            "Select u " +
            "from UpcomingMatchesFacts u " +
            "order by abs(formula) desc ")
    List<UpcomingMatchesFacts> getHighestFormulaMatches();

    List<UpcomingMatchesFacts> findAll();
}

This is my Entity:

@Entity
@Table(name = "upcomingmatchesfacts")
public class UpcomingMatchesFacts {

    public UpcomingMatchesFacts(String firstPlayerName, String secondPlayerName, int matchesWonFirstPlayer,
                                int matchesWonSecondPlayer, int currentRankFirstPlayer, int currentRankSecondPlayer,
                                int ageFirstPlayer, int ageSecondPlayer, double formula) {
        this.firstPlayerName = firstPlayerName;
        this.secondPlayerName = secondPlayerName;
        this.matchesWonFirstPlayer = matchesWonFirstPlayer;
        this.matchesWonSecondPlayer = matchesWonSecondPlayer;
        this.currentRankFirstPlayer = currentRankFirstPlayer;
        this.currentRankSecondPlayer = currentRankSecondPlayer;
        this.ageFirstPlayer = ageFirstPlayer;
        this.ageSecondPlayer = ageSecondPlayer;
        this.formula = formula;
    }

And this is the class where I want to use the method:

@EnableJpaRepositories
public class ChooseBestMatches {

    @Autowired
    private UpcomingMatchesFactsRepository upcomingMatchesFactsRepository;


    public void addBestMatchesToDatabase() {
        List<UpcomingMatchesFacts> upcomingMatchesFacts = Lists.newArrayList(upcomingMatchesFactsRepository.findAll());

                //upcomingMatchesFactsRepository.getHighestFormulaMatches().subList(0, 9);

        }
    }
Maria1995
  • 439
  • 1
  • 5
  • 21

1 Answers1

1

If you want to inject spring bean into other class, it must be a spring bean too. Use @Service annotation on ChooseBestMatches class.

Advice: @EnableJpaRepositories should be on a Configuation class, not on a service. Entities must have noarg constructor.

zlaval
  • 1,941
  • 1
  • 10
  • 11