title: FragmentManager on a null object reference
body: <p>Null object reference exception coming on this line  <code>mFragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();</code></p>
  
  <p>The below code is trying to add <code>TabActivity</code> in <code>Mainactivity</code>. But in <code>TabFragment</code> its throwing null exception. I know that, issue is will my <code>TabFragment</code>. Below is my <code>MainActivty.java</code> and after that, there will be <code>TabFragment.java</code></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  DrawerLayout mDrawerLayout;
  NavigationView mNavigationView;
  FragmentManager mFragmentManager;
  FragmentTransaction mFragmentTransaction;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      /**
       *Setup the DrawerLayout and NavigationView
       */
  
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
      mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
  
      /**
       * Lets inflate the very first fragment
       * Here , we are inflating the TabFragment as the first Fragment
       */
  
      mFragmentManager = getSupportFragmentManager();
      mFragmentTransaction = mFragmentManager.beginTransaction();
      mFragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();
      /**
       * Setup click events on the Navigation View Items.
       */
  
      mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
          @Override
          public boolean onNavigationItemSelected(MenuItem menuItem) {
              mDrawerLayout.closeDrawers();
  
  
  
              if (menuItem.getItemId() == R.id.nav_item_sent) {
                  FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                  fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();
  
              }
  
              if (menuItem.getItemId() == R.id.nav_item_inbox) {
                  FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                 // xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
              }
  
              return false;
          }
  
      });
  
      /**
       * Setup Drawer Toggle of the Toolbar
       */
  
      android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
      ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
              R.string.app_name);
  
      mDrawerLayout.setDrawerListener(mDrawerToggle);
  
      mDrawerToggle.syncState();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>TabFragment.java</p>
  
  <pre><code>public class TabFragment extends Fragment {
  
  public static TabLayout tabLayout;
  public static ViewPager viewPager;
  public static int int_items = 3 ;
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      /**
       *Inflate tab_layout and setup Views.
       */
      View x =  inflater.inflate(R.layout.tab_layout,null);
      tabLayout = (TabLayout) x.findViewById(R.id.tabs);
      viewPager = (ViewPager) x.findViewById(R.id.viewpager);
  
      /**
       *Set an Apater for the View Pager
       */
      viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
  
      /**
       * Now , this is a workaround ,
       * The setupWithViewPager dose't works without the runnable .
       * Maybe a Support Library Bug .
       */
  
     tabLayout.post(new Runnable() {
          @Override
          public void run() {
              tabLayout.setupWithViewPager(viewPager);
          }
      });
  
      return x;
  
  }
  
  class MyAdapter extends FragmentPagerAdapter {
  
      public MyAdapter(FragmentManager fm) {
          super(fm);
      }
  
      /**
       * Return fragment with respect to Position .
       */
  
      @Override
      public Fragment getItem(int position)
      {
          switch (position){
              case 0 : return new PrimaryFragment();
             // case 1 : return new SocialFragment();
             // case 2 : return new UpdatesFragment();
          }
          return null;
      }
  
      @Override
      public int getCount() {
  
          return int_items;
  
      }
  
      /**
       * This method returns the title of the tab according to the position.
       */
  
      @Override
      public CharSequence getPageTitle(int position) {
  
          switch (position){
              case 0 :
                  return "Primary";
              case 1 :
                  return "Social";
              case 2 :
                  return "Updates";
          }
          return null;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help me to correct this code.</p>
  
  <p>error which i am getting is this. </p>
  
  <pre><code>tabproject E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.kpgames.tabproject, PID: 7410
  java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
      at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:418)
      at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:413)
      at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1157)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
      at android.view.View.measure(View.java:18788)
      at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
      at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
      at android.view.View.measure(View.java:18788)
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
      at android.view.Choreographer.doFrame(Choreographer.java:606)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
      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.refl
  </code></pre>