title: NullPointerException while using dagger-2 to inject preferences in fragment
body: <p>I am getting error in injecting preferences:</p>
  
  <p>Can't figure out the problem.</p>
  
  <p>Here are some of the classes i am using :</p>
  
  <p>The AppModule class</p>
  
  <pre><code> @Module
      public class AppModule {
          private final MyApplication app;
  
          public AppModule(MyApplication app) {
              this.app = app;
          }
  
          @Provides
          @Singleton
          public Context provideContext() {
              return app;
          }
  
          @Provides
          @Singleton
          SharedPreferences providesSharedPreference() {
              return PreferenceManager.getDefaultSharedPreferences(app);
          }
      }
  
  // The AppComponent interface
  
      @Singleton
      @Component(
              modules = {
                      AppModule.class,
              }
      )
  
      public interface AppComponent {
  
          void inject(DisplayFragment fragment);
  
      }
  </code></pre>
  
  <p>THE APPLICATION class</p>
  
  <pre><code>public class MyApplication extends Application {
  
      private static MyApplication instance = new MyApplication();
      private static AppComponent appComponent;
  
      public static MyApplication getInstance() {
          return instance;
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          getAppComponent();
      }
  
      public AppComponent getAppComponent() {
          if (appComponent == null) {
              appComponent = DaggerAppComponent.builder()
                      .appModule(new AppModule(this))
                      .build();
          }
          return appComponent;
      }
  }
  </code></pre>
  
  <p>Finally in a fragment i am doing :</p>
  
  <pre><code>public class DisplayFragment extends Fragment implements View.OnClickListener {
  ...
      @Inject
      SharedPreferences prefs;
  
      @Inject
      public DisplayFragment() {
          MyApplication.getInstance().getAppComponent().inject(this);
      }
  ..
  }
  </code></pre>
  
  <p>I am getting this error and application is crashing.</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                         at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
                                                         at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:390)
                                                         at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:385)
                                                         at dagger.module.AppModule.providesSharedPreference(AppModule.java:34)
                                                         at dagger.module.AppModule_ProvidesSharedPreferenceFactory.get(AppModule_ProvidesSharedPreferenceFactory.java:23)
                                                         at dagger.module.AppModule_ProvidesSharedPreferenceFactory.get(AppModule_ProvidesSharedPreferenceFactory.java:8)
                                                         at dagger.internal.ScopedProvider.get(ScopedProvider.java:47)
                                                         at ui.DisplayFragment_MembersInjector.injectMembers(DisplayFragment_MembersInjector.java:29)
                                                         at ui.DisplayFragment_MembersInjector.injectMembers(DisplayFragment_MembersInjector.java:8)
                                                         at dagger.component.DaggerAppComponent.inject(DaggerAppComponent.java:45)
                                                         at ui.DisplayFragment.&lt;init&gt;(DisplayFragment.java:37)
                                                         at ui.DisplayFragment.newInstance(DisplayFragment.java:41)
                                                         at ui.MainActivity$MyPagerAdapter.getItem(MainActivity.java:86)
                                                         at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:101)
                                                         at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
                                                         at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
                                                         at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                         at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:825)
                                                         at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:511)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                                                         at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
                                                         at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                                                         at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                                                         at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                                                         at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
                                                         at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
                                                         at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                                                         at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
                                                         at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2694)
                                                         at android.view.View.measure(View.java:19324)
                                                         at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2545)
                                                         at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1454)
                                                         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1709)
                                                         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1335)
                                                         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6816)
                                                         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
                                                         at android.view.Choreographer.doCallbacks(Choreographer.java:696)
                                                         at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
                                                         at android.os.Handler.handleCallback(Handler.java:815)
                                                         at android.os.Handler.dispatchMessage(Handler.java:104)
                                                         at android.os.Looper.loop(Looper.java:207)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5896)
                                                      at ja
  </code></pre>
  
  <p>Someone please help..</p>