title: Trying to call getApplicationContext() on instance of Application class leads to null object reference error
body: <p>I'm trying to do some networking in my Android app.</p>
  
  <p>The sequence of events that leads up to my error are as follows: </p>
  
  <p>In my fragment, I get a new instance of my VolleySingleton class if one does not already exist. </p>
  
  <pre><code>public static VolleySingleton getInstance() {
          if (vInstance == null) {
              vInstance = new VolleySingleton();
          }
          return vInstance;
      }
  </code></pre>
  
  <p>In creating an instance of VolleySingleton, I create a request queue and set it equal to Volley.newRequestQueue(myApplication.getContext());</p>
  
  <pre><code>    public class VolleySingleton {
      private static VolleySingleton sInstance = null;
      private ImageLoader imageLoader;
      private RequestQueue mRequestQueue;
      MyApplication myApplication = new MyApplication();
  
      private VolleySingleton() {
          mRequestQueue = Volley.newRequestQueue(myApplication.getAppContext());
          imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
              private LruCache&lt;String, Bitmap&gt; cache = new LruCache&lt;&gt;((int)(Runtime.getRuntime().maxMemory()/1024/8));
              @Override
              public Bitmap getBitmap(String url) {
                  return cache.get(url);
              }
  
              @Override
          public void putBitmap(String url, Bitmap bitmap) {
                  cache.put(url, bitmap);
              }
          });
      }
  
      public static VolleySingleton getInstance() {
          if (sInstance == null) {
              sInstance = new VolleySingleton();
          }
          return sInstance;
      }
  
      public RequestQueue getRequestQueue() {
          return mRequestQueue;
      }
  
      public ImageLoader getImageLoader() {
          return imageLoader;
      }
  }
  </code></pre>
  
  <p>Now, myApplication is just an instance of the MyApplication class which extends Application, and getContext() simply calls getApplicationContext on an instance of MyApplication. Here is the code for the MyApplication class:</p>
  
  <pre><code>public class MyApplication extends Application {
  
      private static MyApplication sInstance;
      public static final String API_KEY = "xxxxxxxx";
  
      @Override
      public void onCreate() {
          super.onCreate();
          sInstance=this;
      }
  
      public static MyApplication getInstance() {
          return sInstance;
      }
  
      public Context getAppContext() {
          return sInstance.getApplicationContext();
      }
  
  
  }
  </code></pre>
  
  <p>The error seems to occur when I try to <code>return sInstance.getApplicationContext();</code></p>
  
  <p>I've managed to get identical code to work in a separate test project, so I'm at a loss here.</p>
  
  <p>Here is my logcat:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.xxxxxxxxx.chessgame.Models.MyApplication.getApplicationContext()' on a null object reference
              at com.xxxxxxxxx.chessgame.Models.MyApplication.getAppContext(MyApplication.java:28)
              at com.xxxxxxxxxx.chessgame.Networking.VolleySingleton.&lt;init&gt;(VolleySingleton.java:25)
              at com.xxxxxxxxx.chessgame.Networking.VolleySingleton.getInstance(VolleySingleton.java:42)
              at com.xxxxxxxx.chessgame.RecyclerViews.FragmentViewOne.onCreate(FragmentViewOne.java:95)
              at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
              at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:842)
              at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:859)
              at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:152)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
              at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
              at android.view.View.measure(View.java:17430)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
              at android.view.View.measure(View.java:17430)
              at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
              at android.view.View.measure(View.java:17430)
              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
              at android.view.Choreographer.doCallbacks(Choreographer.java:580)
              at android.view.Choreographer.doFrame(Choreographer.java:550)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.Act
  </code></pre>