Yogesh Gavali
Yogesh Gavali

Reputation: 61

Getting repository object null in springboot

I have below project structure

https://i.sstatic.net/CYXND.png

My code files are as belows

DemoApplication.java

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        CarListGrabber grabber = new CarListGrabber();
        grabber.grabCarsWithName("Test");
    }

}

NewCarRepository.java

@Repository
public interface NewCarRepository extends CrudRepository<NewCar, Long> {

}

NewCar.java

@Entity
@Table(name = "new_car_details")
public class NewCar {
    // member variables with default constructor
}

CarListGrabber.java

@Service
public class CarListGrabber {

    @Autowired
    private NewCarRepository newCarRepository;

    // someOtherStuff
}

Even though I have used annotations @Repository, @Service I am getting null repository object in service.

Upvotes: 0

Views: 556

Answers (1)

pirho
pirho

Reputation: 12245

You are instantiating a new CarListGrabber with:

CarListGrabber grabber = new CarListGrabber();

it will not make injections you need to inject also your grabber, like:

@Autowired
CarListGrabber grabber;

Upvotes: 1

Related Questions