title: Not able to add the fragments using Kotlin Extension function
body: <p>I have a Kotlin extension function to add a fragment which is in another Kotlin file</p>
  
  <pre><code>fun Fragment.addFragment(tag: String?, id: Int, fragmentManager: FragmentManager) {
    // will take care of adding the fragment.
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.add(id, this, tag)
    fragmentTransaction.addToBackStack(tag)
    fragmentTransaction.commit()
  }
  </code></pre>
  
  <p>But whenever I am using that extension function to add a fragment then I am getting this crash</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference
  </code></pre>
  
  <p>I am using that extension function like this:</p>
  
  <pre><code>MyFragment().addFragment("MyFragment", R.id.frame, fragmentMaganer)
  </code></pre>
  
  <p>After the above statement if I tried to debug my code then its executed successfully till</p>
  
  <pre><code>fragmentTransaction.commit()
  </code></pre>
  
  <p>but after that in the <strong>onStart()</strong> method of my activity I am facing that NullPointerException issue</p>
  
  <p>But if I use the same code directly in Activity like this:</p>
  
  <pre><code>val myFragment = MyFragment()
  val fragmentTransaction = fragmentManager.beginTransaction()
  fragmentTransaction.add(R.id.frame, myFragment, tag)
  fragmentTransaction.addToBackStack(tag)
  fragmentTransaction.commit()
  </code></pre>
  
  <p>then Its working fine. Can someone please explain why its no working with <strong>Extension function</strong>?</p>