3

We are periodically running into this issue with our Dagger 2 components. It seems to be occurring after a period of idle. It is not always reproducible. Here is the stacktrace:

--------- beginning of crash
05-19 14:06:22.145 31322-31322/org.mythtv.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.mythtv.android, PID: 31322
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object org.mythtv.android.presentation.internal.di.HasComponent.getComponent()' on a null object reference
    at org.mythtv.android.app.view.fragment.AbstractBaseFragment.getComponent(AbstractBaseFragment.java:67)
    at org.mythtv.android.app.view.fragment.EncoderListFragment.initialize(EncoderListFragment.java:139)
    at org.mythtv.android.app.view.fragment.EncoderListFragment.reload(EncoderListFragment.java:194)
    at org.mythtv.android.app.view.activity.MainActivity$1.onClick(MainActivity.java:115)
    at android.view.View.performClick(View.java:5204)
    at android.view.View$PerformClick.run(View.java:21153)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Here is the code that is blowing up:

/**
 * Gets a component for dependency injection by its type.
 */
@SuppressWarnings( "unchecked" )
protected <C> C getComponent( Class<C> componentType ) {

    return componentType.cast( ( (HasComponent<C>) getActivity() ).getComponent() );
}

This is called when the fragments initialize. The key item here is that these fragments are members of a ViewPager. The fragments are created and initialized as a part of the constructor of the FragmentStatePagerAdapter:

    public MainFragmentPagerAdapter( FragmentManager fm ) {
        super( fm );

        tabs = getResources().getStringArray( R.array.main_tabs );
        fragments.add( Fragment.instantiate( MainActivity.this, RecentListFragment.class.getName(), null ) );
        fragments.add( Fragment.instantiate( MainActivity.this, EncoderListFragment.class.getName(), null ) );
        fragments.add( Fragment.instantiate( MainActivity.this, UpcomingListFragment.class.getName(), null ) );

        notifyDataSetChanged();
    }

There is a FloatingActionButton on that refreshes the currently selected fragment in the ViewPage. This seems to be firing when activity resumes.

Any ideas on how to prevent this NPE?

dmfrey
  • 1,230
  • 1
  • 17
  • 33
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –  May 22 '16 at 16:09
  • Have a look at your activity. The component gets nulled there. Probably because of the activity lifecycle and wrong inizialization of the activity. – David Medenjak May 22 '16 at 17:07
  • I've added some checking. I think it's an issue with timing in how these fragments are getting recreated after being woken up. So now, it shouldn't fire these refreshes until someone actually hits the FAB. – dmfrey May 23 '16 at 16:52

0 Answers0