title: Android : Replacing a fragment inside an Activity's onPause() method gives NullPointerException
body: <p>I am trying to learn fragments in android and trying to replace fragment with another one in the Activity's <code>onPause()</code> method when the activity pauses.</p>
  
  <p>My code for onPause method is :-</p>
  
  <pre><code>public void onPause(){
      super.onPause();
      System.out.println("ActivityA-onPause()-1");
      ArticleListFragment temp=(ArticleListFragment) getFragmentManager().findFragmentById(R.id.article_list_fragment);
      System.out.println("ActivityA-onPause()-2");
      if(!temp.isAdded()){    //problem
          System.out.println("ActivityA-onPause()-3");
          fragmentTransaction.replace(R.id.activity_a, articleReaderFragment, new String("readerFragment"));
          System.out.println("ActivityA-onPause()-4");
      }
      System.out.println("ActivityA-onPause()-5");
  }
  </code></pre>
  
  <p>When i execute the app on device, onPause() executes but prints only till " Activity-onPause()-2 " but does not print " Activity-onPause()-3 ".</p>
  
  <p>I get error log as :-</p>
  
  <pre><code>06-13 00:25:10.946: I/System.out(17974): ActivityA-onPause()-1
  06-13 00:25:10.946: I/System.out(17974): ActivityA-onPause()-2
  06-13 00:25:10.946: D/AndroidRuntime(17974): Shutting down VM
  06-13 00:25:10.946: W/dalvikvm(17974): threadid=1: thread exiting with uncaught exception (group=0xb4e04908)
  06-13 00:25:10.961: E/AndroidRuntime(17974): FATAL EXCEPTION: main
  06-13 00:25:10.961: E/AndroidRuntime(17974): java.lang.RuntimeException: Unable to pause activity {com.pravesh.fragments/com.pravesh.fragments.ActivityA}: java.lang.NullPointerException
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3016)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread.access$800(ActivityThread.java:141)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.os.Handler.dispatchMessage(Handler.java:99)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.os.Looper.loop(Looper.java:137)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread.main(ActivityThread.java:5039)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at java.lang.reflect.Method.invokeNative(Native Method)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at java.lang.reflect.Method.invoke(Method.java:511)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at dalvik.system.NativeStart.main(Native Method)
  06-13 00:25:10.961: E/AndroidRuntime(17974): Caused by: java.lang.NullPointerException
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at com.pravesh.fragments.ActivityA.onPause(ActivityA.java:37)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.Activity.performPause(Activity.java:5206)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
  06-13 00:25:10.961: E/AndroidRuntime(17974):    ... 12 more
  </code></pre>
  
  <p>All I can understand is that it gives a null pointer exception but I don't know why.</p>