I have been trying to solve this and have looked through lot of tutorials.Could you please help in solving the below problem
I am getting NPE on line "AndroidInjection.inject(this);"
Caused by: java.lang.NullPointerException: com.example.vipulthakur.testprojectmvvm.TestProjectMVVMApplication.activityInjector() returned null
at dagger.internal.Preconditions.checkNotNull(Preconditions.java:79) at dagger.android.AndroidInjection.inject(AndroidInjection.java:56) at com.example.vipulthakur.testprojectmvvm.MainActivity.onCreate(MainActivity.java:50) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)聽 at android.app.ActivityThread.-wrap12(ActivityThread.java)聽 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)聽 at android.os.Handler.dispatchMessage(Handler.java:102)聽 at android.os.Looper.loop(Looper.java:154)聽at android.app.ActivityThread.main(ActivityThread.java:6077)聽 at java.lang.reflect.Method.invoke(Native Method)聽 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)聽
public class TestProjectMVVMApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerApplicationComponent
.builder()
.application(this)
.build()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingActivityInjector;
}
}
Application Component:
@Component(modules = {AndroidInjectionModule.class, ApplicationModule.class, MapActivityBuilder.class})
public interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
ApplicationComponent build();
}
void inject(Application app);
}
ActivityBuilder Class:
@Module(subcomponents = ActivityComponent.class)
abstract class MapActivityBuilder {
@Binds
@IntoMap
@ActivityKey(MainActivity.class)
abstract AndroidInjector.Factory<? extends Activity> bindActivity(ActivityComponent.Builder builder);
}
This appears to be a question - please use StackOverflow with the dagger-2 tag instead, as that is a better forum for questions.
Possibly related to #748 (though in this case, the application would be non-null, but onCreate likely wouldn't have been called on it)
In application component class just instead injecting Application class change to inject your application class in this particular case TestProjectMVVMApplication and it should work.
Hope you had added application class name in the Android Manifest file. Without it, the app will work but the dagger component will be null.
Most helpful comment
In application component class just instead injecting Application class change to inject your application class in this particular case TestProjectMVVMApplication and it should work.