I am converting my MavenProject to a SpringBootApplication. Up until I try to use the findById()-Method from JpaRepository point it is running.

My Repository:

package demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import demo.model.Datenherkunft;

@Repository
public interface RepositoryDatenherkunft extends JpaRepository<Datenherkunft, Integer>{}

My Class that is trying to use the Method:

package demo.service;

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

import demo.model.*;
import demo.repository.*;

@Service
public class ForeignKeyObjects implements ServiceGet{

    @Autowired
    private RepositoryDatenherkunft rDatenherkunft;

    @Override
    public Datenherkunft getDatenherkunft(Integer datenherkunft) {
        // TODO Auto-generated method stub
        return rDatenherkunft.findById(datenherkunft).get();
    }

The Exception

java.lang.NullPointerException: Cannot invoke "demo.repository.RepositoryDatenherkunft.findById(Object)" because "this.rDatenherkunft" is null
    at demo.service.ForeignKeyObjects.getDatenherkunft(ForeignKeyObjects.java:34) ~[classes/:na]
    at demo.service.ListenInhaltValidation.listenInhaltValidation(ListenInhaltValidation.java:15) ~[classes/:na]
    at demo.service.DtoValidation.dtoValidation(DtoValidation.java:15) ~[classes/:na]
    at demo.service.ServiceImp.createobjects(ServiceImp.java:50) ~[classes/:na]
    at demo.controller.Controller.createObjects(Controller.java:29) ~[classes/:na]
    ...

I am very new to coding and SpringBoot. So my project is a bit all over the place. The reason the class is not a controller but a service is that i have a few classes which function to validate and convert the data before it gets stored in my mysql database. So in my understanding this functionality should be in the service layer.

I tried:

  • @Controller and not @Service
  • putting @Autowired above every variable
  • putting @Repository above the Repository

Nothing changed the outcome.

0

There are 0 best solutions below