title: How do I initialize a collection value for a MutableLiveData object?
body: <pre><code>class Foo : ViewModel() {
     val bars: MutableLiveData&lt;ArrayList&lt;Bar&gt;&gt; = MutableLiveData()
       get() {
         if(field.value == null) {
           field.setValue(ArrayList()) // NullPointerException
         }
       }
  }
  
  class FooTest(){
     @Test fun itShouldNotBlowUp() {
       Foo() //nullPointerException
  
     }
  
  }
  </code></pre>
  
  <p>I don't understand how to initialize the value of a MutableLiveData object. I've tried to lazily initialize it via the getter and with an <code>init</code> block. Both approaches throw a null pointer when setting the value. <code>bars</code> is not null however. </p>
  
  <p>Here is the stacktrace:</p>
  
  <pre><code>java.lang.NullPointerException
  at android.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:58)
  at android.arch.core.executor.ArchTaskExecutor.isMainThread(ArchTaskExecutor.java:116)
  at android.arch.lifecycle.LiveData.assertMainThread(LiveData.java:434)
  at android.arch.lifecycle.LiveData.setValue(LiveData.java:279)
  at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
  at org.Foo.&lt;init&gt;(Foo.kt:10)
  at org.FooTest.ShouldNotBlowUp(FooTest.kt:3)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  </code></pre>
  
  <p>How do I initialize the ArrayList and set it as the value of <code>bars</code>?</p>