title: How to check if an index is empty/null in a List in Kotlin?
body: <p>I wanted to check if an <code>index</code> of an <code>array</code> is empty/null, and if it's, then I wanted to add a new item at that index otherwise replace the old item with the new item (I do not want to keep the old item in the array).</p>
  <p>I tried the following code but that didn't work</p>
  <pre><code>    private var mSelectedImageFileUriList: ArrayList&lt;Uri?&gt;? = null
  mSelectedImageFileUriList= ArrayList(5)
  
          if (mSelectedImageFileUriList?.get(1) ===null){
                              mSelectedImageFileUriList?.add(data.data!!)
                          }else{
                              mSelectedImageFileUriList?.set(1,data.data!!)
                          }
  </code></pre>
  <hr />
  <p><code>mSelectedImageFileUriList?.add(data.data!!)</code> is the code I previously used and it worked without errors. Then I try with <code>mSelectedImageFileUriList?.set(0,data.data!!)</code>, the app crashes. I am checking if the position (index) is <code>null</code> because there are chances it's already occupied and if it's occupied then I do not want the old data in it, instead, I want to replace it with the new one (new one should be at the same position.</p>
  <p>It's basically for creating an array of <code>uri</code>. When the user wants to change the image, that is when I want to check if, at that <code>index</code>, the user had already selected an image and if he has, then the old <code>uri</code> must be removed and the new one should be added in the same position in the <code>array</code>.</p>
  <p>There are 5 images, the size of the <code>array</code> is limited to 5 (<code>mSelectedImageFileUriList= ArrayList(5)</code>). When a user adds 'image1' the <code>uri</code> is stored at 'index0', and when he adds 'image2' the <code>uri</code> is stored at 'index1'. After adding 'image2' ('index1'), the user wants to edit (change) the image he added for 'image1', if I use <code>mSelectedImageFileUriList?.add(data.data!!)</code> it doesn't remove the old one and the new <code>uri</code> is added at the bottom on the array but I want it to be at the same position, in this case, 'index0'. I hope it's clear.</p>
  <p>I shall add the complete code if required.</p>
  <p><code>logcat</code></p>
  <pre><code>        Process: com.trad, PID: 21555
      java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/76207 flg=0x1 (has extras) }} to activity {com.trad/com.trad.ui.activities.AddProductActivity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
          at android.app.ActivityThread.deliverResults(ActivityThread.java:5506)
          at android.app.ActivityThread.handleSendResult(ActivityThread.java:5547)
          at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:246)
          at android.app.ActivityThread.main(ActivityThread.java:8512)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
       Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
          at java.util.ArrayList.set(ArrayList.java:453)
          at com.trad.ui.activities.AddProductActivity.onActivityResult(AddProductActivity.kt:354)
          at android.app.Activity.dispatchActivityResult(Activity.java:8541)
          at android.app.ActivityThread.deliverResults(ActivityThread.java:5499)
          at android.app.ActivityThread.handleSendResult(ActivityThread.java:5547) 
          at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:246) 
          at android.app.ActivityThread.main(ActivityThread.java:8512) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
  2021-07-11 14:33:46.867 637-1917/? E/BufferQueueProducer: [Application Error: com.trad#0](id:27d00004287,api:0,p:-1,c:637) disconnect: not connected (req=1)
  </code></pre>