title: Non-null value in Kotlin produces nullpointerexception
body: <p>I have an array of non-nullable values like this:  </p>
  
  <pre><code>private val dogs = ArrayList{Dog}() // replace {} with &lt;&gt;
  </code></pre>
  
  <p>It is populated like this:  </p>
  
  <pre><code>val dog = Dog()  
  dog.name = “bob”  
  dogs.add(dog)  
  </code></pre>
  
  <p>There are up to 3 different methods that can populate it like this, depending on the code flow.
  Once the array is filled, it is processed:</p>
  
  <pre><code>if (dogs.size &gt; 0) {  
      Collections.sort(dogs, Dog.comparator)  
      for (i in dogs.indices) {  
          val dog = dogs[i]  
          dog.bark()  
      }  
  }
  </code></pre>
  
  <p>All of this is done in AsyncTask doInbackground.</p>
  
  <p>I’m intermittently getting the following exception when calling dog.bark():</p>
  
  <p>Fatal Exception: java.lang.RuntimeException
  An error occurred while executing doInBackground()</p>
  
  <pre><code>Caused by java.lang.NullPointerException: Attempt to invoke virtual method com.myname.model.Dog.P()' on a null object reference
         at com.myname.util.RunnableAsync.doInBackground(SourceFile:5)
         at android.os.AsyncTask$2.call(AsyncTask.java:333)
         at java.util.concurrent.FutureTask.run(FutureTask.java:266)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
         at java.lang.Thread.run(Thread.java:764)
  </code></pre>
  
  <p>In short, ArrayList of non-null objects sometimes contains a null object.
  I’m trying to figure out how this can happen. I would expect  that Kotlin would complain if non-null value is added to the ArrayList and of course I’m not sure how this can happen in the first place as I only put non-null values in the array. Threading issues? Any ideas are welcome:) Thanks.</p>