title: How to write a recursive coroutines in kotlin
body: <p>I would like to write a faster File Searcher in kotlin to find all text files under my home folder. </p>
  
  <p>Here is my code with sequence search</p>
  
  <pre><code>import java.io.File
  import java.util.concurrent.atomic.AtomicLong
  
  var count: AtomicLong = AtomicLong(0)
  
  fun main(args: Array&lt;String&gt;) {
    println(System.getProperty("user.home"))
  
    val start = System.currentTimeMillis()
    findTxtFile(System.getProperty("user.home") + "/git", ".txt")
  
    println("took: " + (System.currentTimeMillis() - start))
  
    println(count.get())
  }
  
  
  fun findTxtFile(path: String, extension: String) {
    File(path).listFiles().forEach {
      if (it.isDirectory) {
        findTxtFile(it.absolutePath, extension)
      } else if (it.name.endsWith(extension)) {
        count.getAndIncrement()
      }
    }
  }
  </code></pre>
  
  <p>I wrote a program following, however when you have a lot of file, it throw NullPointerException, how could it possible throw null pointer Exception?</p>
  
  <pre><code>import kotlinx.coroutines.experimental.CommonPool
  import kotlinx.coroutines.experimental.async
  import kotlinx.coroutines.experimental.runBlocking
  import java.io.File
  
  fun main(args: Array&lt;String&gt;) {
      val start = System.currentTimeMillis()
      var filePath = System.getProperty("user.home")
      var deferred = File(filePath).listFiles().map { path -&gt;
          async(CommonPool) {
              findTxtFile(path.absolutePath)
          }
      }
  
      runBlocking {
          deferred.map {
              it.await()
          }
      }
      println((System.currentTimeMillis() - start) / 1000)
  }
  
  suspend fun findTxtFile(path: String) {
      //println("--------------------- $path")
      if (File(path).isDirectory) {
          File(path).listFiles().forEach {
              findTxtFile(it.absolutePath) //===&gt; Exception happen here
          }
      } else if (path.endsWith(".java")) {
          println(path)
      }
  }
  </code></pre>