title: Android Access to view model instance in properties
body: <p>HomeFragment.tk :</p>
  <pre><code>class HomeFragment : Fragment() {
  
  private val questionViewModel: QuestionListViewModel by viewModels {
      
      InjectorUtils.provideQuestionListViewModelFactory(this)
  }
  
  var allQuestionsCount :LiveData&lt;Int&gt;  = questionViewModel.getCountQuestions()
    
   ... ELSE CODE
  }
  </code></pre>
  <p>If i use <code>questionViewModel</code> for initializing other properties(like the above code), get the error:</p>
  <pre><code>       at android.app.ActivityThread.main(ActivityThread.java:6077)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
       Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #11: Error inflating class fragment
       Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
       Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.myapplication.ui.home.HomeFragment: calling Fragment constructor caused an exception
          at androidx.fragment.app.Fragment.instantiate(Fragment.java:566)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
  2020-10-25 16:48:29.026 28597-28597/com.example.android.myapplication E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
          at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
          at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
          at com.example.android.myapplication.MainActivity.onCreate(MainActivity.kt:29)
          at android.app.Activity.performCreate(Activity.java:6662)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
          at android.app.ActivityThread.-wrap12(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6077)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Constructor.newInstance0(Native Method)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
          at androidx.fragment.app.Fragment.instantiate(Fragment.java:548)
              ... 49 more
       Caused by: java.lang.IllegalStateException: Fragment HomeFragment{ad9f957} (58527bd1-4b47-493e-a080-0cc3920ea395)} not attached to a context.
  </code></pre>
  <p>But if instead i replace the <code>allQuestionsCount</code> property with :</p>
  <pre><code>lateinit var allQuestionsCount :LiveData&lt;Int&gt;
  </code></pre>
  <p>and then inside <code>onCreateView</code> initialize it:</p>
  <pre><code> allQuestionsCount = questionViewModel.getCountQuestions()
  </code></pre>
  <p>now there is no error.I need to know whats wrong with the first approach?</p>