Android Dagger @Injects are null

The dagger kicks my ass.

I have the following layout.

  • The main function that is introduced into the object graph.
  • MainActivity @Injects interface MainPresenter. This is provided through the MainModule, which returns a concrete implementation.
  • The concrete implementation of MainPresenter takes the FileContentInteractor interface. This is provided by the InteractorModule, which returns a concrete implementation of the FileContentInteractor.
  • MainModule can do it because it includes InteractorModule.class

Everything is fine so far. From this point on, it becomes difficult.

The concrete implementation of FileContentInteractor injects some member variables using @Inject. These member variables are all interfaces to which concrete implementations are exposed through their respective modules.

An example is @Inject ThreadExecutor threadExecutor. I thought it would be provided because:

  • InteractorModule includes ExecutorModule.class.
  • ExecutorModule.class @ Provides a concrete implementation of ThreadExecutor as a TaasExecutor object.
  • The TaskExecutor has no nested dependencies.

When starting the app and MainActivity, the following opens:

  • MainActivity calls the MainPresenter implementation method - works great!
  • MainPresenter has a FileContentInteractor implementation and calls a method on it - it works
  • A method in the FileContentInteractor implementation tries to call a method in the ThreadExecutor implementation (which is @Injected as a member variable). It failed because @Injected ThreadExecutor's implementation is null.

Can anyone please help?


source to share

1 answer

Okay, so it turns out I was kicking my butt ...

I have referenced concrete classes with my @Inject members, but used interfaces like my @Provides.

Changing @Inject members from classes to interfaces is fixed.



All Articles