3

I have a problem with the Google Guice framework.

I'm trying to create a simple application that injects a list of objects. Unfortunately, when trying to run an application, I get the following error.

No implementation for java.util.List was bound. while locating java.util.List for field at Operator.carShops(Operator.java:17) while locating Operator ()

Here is the program code:

public class Main {
public static void main(String[] args) {
        Injector injector = Guice.createInjector();
        Operator operator = injector.getInstance(Operator.class);

        operator.prepareData();
}}



public class Operator implements IOperator {
@Inject
private List<CarShop> carShops;

public List<CarShop> getCarShops() {
    return carShops;                             <--- Place of error occurrence
}

public void setCarShop(List<CarShop> carShops) {
    this.carShops = carShops;
}

public void prepareData() { 
    for(CarShop carShop:carShops)
        for(int i=0;i<10;i++) {
            Car car = new Car();
            car.setPrice(1000);     
            carShop.addCar(car);
        }
}}

Please help

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Rafal
  • 31
  • 1
  • 2

1 Answers1

1

It seems your module registering dependencies is missing. You need to tell Guice what class will be used when it is asked for an interface.

import com.google.inject.AbstractModule;

public class SimpleModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(CarShop.class).to(CarShopImpl.class);
    }
}

Where CarShopImpl is a particular implementation for CarShop interface.

Let's say, the start of CarShopImpl class should be:

public class CarShopImpl implements CarShop {

    // Implementation
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183