Dagger Application injection NPE
See original GitHub issueI’m trying to inject some objects on application, in order to access them whenever I want. So, on my application I’m doing something like this:
private static AppComponent mApplicationComponent;
@Override
public void onCreate() {
…
mApplicationComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this, new ModuleSpref(this))
.build();
}
public static AppComponent getInstance() {
return mApplicationComponent;
}
However, according to google crash log of my production app, the application has a NullPointException when trying to access anything from getInstance(). On my application this happens on a service which is only called via a receiver. Additionally, this also happens on another receiver.
MyApplication.getInstance().context(); //Null Point Exception :(
Here is my ApplicationComponent:
@Component(modules = AppModule.class)
@Singleton
public interface AppComponent {
void inject(Context context);
//Exposed to sub-graphs.
Context context();
ModuleSpref getSpref();
}
I tried many times to replicate, but so far without success, despite the google crash saying otherwise. Am I doing something wrong here? As far as I know, for services/receivers to be initialised in android, the application must first be initialised, so getInstance() should not return null right?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:31 (2 by maintainers)
Top GitHub Comments
No
On Tue, Oct 17, 2017, 12:12 PM Ron Shapiro notifications@github.com wrote:
That’s never bothered anyone using Koin, which works exactly the same way.