title: Kotlin arrayList - can't rewrite the list
body: <p>I am using a class named ItemHolder to hold some objects in a List . Dynamically I add some . At a certain point I need to clear the items(all of them) to add new ones . When I try to get an item from list I get the IndexOutOfBoundException .</p>
  
  <p>Here is the code I use :</p>
  
  <p>Inside ItemHolder </p>
  
  <pre><code>    var itemList : ArrayList&lt;Item&gt; = arrayListOf()
  </code></pre>
  
  <p>In another class on init :</p>
  
  <pre><code>    itemHolder.itemList = arrayListOf()
  </code></pre>
  
  <p>And later on the same class :</p>
  
  <pre><code>itemHolder.itemList.add(position, database.getRandomData())
  </code></pre>
  
  <p>and :</p>
  
  <pre><code>itemHolder.getItem(position)
  </code></pre>
  
  <p>The problem is that when I first time use the class it works fine but second time throws me the exception .</p>
  
  <pre><code>FATAL EXCEPTION: main
                                                                     Process: studio.monk.rq, PID: 2812
                                                                     java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                         at java.util.ArrayList.get(ArrayList.java:411)
                                                                         at studio.monk.rq.common.ItemHolder.getItem(ItemHolder.kt:9)
                                                                         at studio.monk.rq.fragments.MainFragment$onCreateView$vpPageChangeListener$1.onPageSelected(MainFragment.kt:44)
                                                                         at studio.monk.rq.fragments.MainFragment$onCreateView$1.run(MainFragment.kt:51)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>