title: Getting list of images from a folder in android kotlin
body: <p>I'm trying to get list of images from a folder using this function</p>
  
  <pre><code>var gpath:String = Environment.getExternalStorageDirectory().absolutePath
  var spath = "testfolder"
  var fullpath = File(gpath + File.separator + spath)
  var list = imageReader(fullpath)
  
  fun imageReader(root : File):ArrayList&lt;File&gt;{
      val a : ArrayList&lt;File&gt; ? = null
      val files = root.listFiles()
      for (i in 0..files.size){
          if (files[i].name.endsWith(".jpg")){
              a?.add(files[i])
          }
      }
      return a!!
  }
  </code></pre>
  
  <p>but i have these exceptions :</p>
  
  <p><em>java.lang.ArrayIndexOutOfBoundsException:length=3;index=3</em></p>
  
  <p><em>kotin.kotlinNullPointerException</em></p>
  
  <p>I've read about this problem but i have no idea how to fix it,</p>
  
  <p>any help please?</p>