-1

I use Dagger in my app. As the documentation says, I instatiate it at my Application class as a private static class member, and get it by a getter where I need it.

    private void initDagger() {
    appComponent = DaggerAppComponent
            .builder()
            .appModule(new AppModule(this))
            .build();
}

But sometimes when call the getter from my SplashActivity (this is my Launcher Activity) appcomponent is null, so I get a NullpointerException when I try to inject SplashActivity into Dagger. I can't reproduce this issue, but I can it in Crashlytics. It maybe happens if the user doesn't close the app, but returns after a long time. In this case I call SplashActivity again and clear the stack, because I have to reinit some things I can do just here. Maybe that's the point when the appcomponent will be null. I can see this issue in just SplashActivity and on just Android 7.x (and some 6.x). Now I try to get appcomponent like a singletone

    public static AppComponent initDaggerAndGet(IndexApplication application){
    if(appComponent == null){
        synchronized (lock){
            if(appComponent==null)appComponent = DaggerAppComponent
                    .builder()
                    .appModule(new AppModule(application))
                    .build();
        }
    }
    return appComponent;
}

when I get it at the SplashActivity and I refactor initDagger() method like this

    private void initDagger() {
    initDaggerAndGet(this);
}

As I wrote, I can't reproduce the issue, and I can't find something like this issue at SO. What do you think what can be the problem? Will the refactored initalization solve it? Thanks!

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
user3057944
  • 701
  • 1
  • 8
  • 19
  • Please include _all_ of the relevant code that is affected as well as the error and the actual stack trace. My guess would be that the activity was destroyed and recreated, and you don't recreate the component for some reason. – David Medenjak Aug 16 '17 at 10:31

1 Answers1

0

finally I was able to reproduce the issue. I started my app, than a lot of other still my app was killed. At this time if I started my app from task manager or by the launcher icon, I get this exception. The private static instance of Appcomponent in my Application class was null. So now I refactored my AppComponent getter, I get it like as a singletone like this

    public static AppComponent getAppComponent() {
    Logger.logAppComponent("");
    if (appComponent == null) {
        Logger.logAppComponent("AppComponent is null");
        synchronized (lock) {
            if (appComponent == null) {
                Logger.logAppComponent("Create still null, so create new AppComponent");
                appComponent = DaggerAppComponent
                        .builder()
                        .appModule(new AppModule(getApplication()))
                        .build();
            }
        }
    }
    Logger.logAppComponent("Return AppComponent");
    return appComponent;
}

rather than init AppComponent in Appliaciton's onCreate(), and now it works.

user3057944
  • 701
  • 1
  • 8
  • 19