title: Scala future strange error on completion
body: <p>I am just getting started with futures and stumbled upon an error which seems quite strange to me:</p>
  
  <p>Using play-ws to perform a post request and map the result:</p>
  
  <pre><code>wsClient.url(url).withHeaders("Content-Type" -&gt; "application/json")
        .post(payload)
        .map { wsResponse =&gt;
          if (!(200 to 299).contains(wsResponse.status)) {
            sys.error(s"Received unexpected status, open-cpu error ${wsResponse.status} : ${wsResponse.body}")
          }
          println(s"OK, received ${wsResponse.body}")
  
          wsResponse.json.validate[Seq[MyClass]] match {
            case JsSuccess(result, _) =&gt; result.map(outlierRes =&gt; Map("key" -&gt; outlierRes.attr, "key2" -&gt; outlierRes.attr2, "key3" -&gt; outlierRes.val3))
            case JsError(error) =&gt; throw new MyException(error.toString())
          }
        }
  </code></pre>
  
  <p>works just fine. The println of the body shows everything is there, and the validation succeeds.</p>
  
  <p>The problem lies here: aggregatedData = <code>Await.result(theFutureFromAbove, 20.minutes)</code></p>
  
  <p>This statement crashes with the following when run via the interactive console:</p>
  
  <pre><code>MyException
          at $anonfun$1.apply(&lt;console&gt;:44)
          at $anonfun$1.apply(&lt;console&gt;:44)
          at scala.util.Success$$anonfun$map$1.apply(Try.scala:206)
          at scala.util.Try$.apply(Try.scala:161)
          at scala.util.Success.map(Try.scala:206)
          at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
          at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
          at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
          at scala.concurrent.impl.ExecutionContextImpl$$anon$3.exec(ExecutionContextImpl.scala:107)
          at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
          at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
          at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
          at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
  </code></pre>
  
  <p>Which would suggest a parsing exception.</p>
  
  <p>However, when run via <code>sbt run</code> there is a different exception:</p>
  
  <pre><code>java.lang.NullPointerException
  </code></pre>
  
  <p>Which resembles this line:     <code>wsClient.url(baseUrl + url).withHeaders("Content-Type" -&gt; "application/json")</code></p>
  
  <h1>edit</h1>
  
  <p>It seems to be triggered by: <code>wsClient.close()</code> as if I closed the wsClient before the future completed. 
  However, in the documentation it states</p>
  
  <blockquote>
    <p>If you create a WSClient manually then you must call client.close() to
    clean it up when you’ve finished with it.</p>
  </blockquote>
  
  <p>so where should I close it? Initially, I thought it would be safe to close after the <code>Await.result</code> but that still is throwing the error.</p>
  
  <h1>edit 2</h1>
  
  <pre><code>wsResponse.json.validate[Seq[MyClass]].fold(
            error =&gt; {
              println(error)
              Future.failed(new MyException("parsing failed" + error))
            },
            result =&gt; result.map(data =&gt; Map("period" -&gt; data.period, "amount" -&gt; data.totalAmount, "outlier" -&gt; data.isOutlier))
          )
  </code></pre>
  
  <p>But this still does not compile as <code>Future[nothing]</code> does not match my return type of <code>Future[Seq[Map[String, Any]]]</code> </p>