title: Creating csv file in Scala
body: <p>I am trying to to create a <code>csv</code> file in Scala by getting data in the form of JsValue from a third part API.
  My method to save data to CSV file is :</p>
  <pre><code>def saveToCSV(str:Seq[JsValue]) = {
    val outputFile = new BufferedWriter(new FileWriter(&quot;Result.csv&quot;))
    val csvWriter = new CSVWriter(outputFile)
      val data = str.head
      data match {
        case JsObject(fields) =&gt; {
          var listOfRecords = new ListBuffer[Array[String]]()
          //val csvFields = Array(&quot;Open&quot;,&quot;High&quot;,&quot;Low&quot;,&quot;Close&quot;,&quot;Volume&quot;)
          //listOfRecords += csvFields
          fields.values.foreach(value =&gt; {
            val jsObject = value.asJsObject()
            val nameList = List(jsObject.fields(&quot;1. open&quot;).toString,jsObject.fields(&quot;2. high&quot;).toString,jsObject.fields(&quot;3. low&quot;).toString,jsObject.fields(&quot;4. close&quot;).toString,jsObject.fields(&quot;5. volume&quot;).toString)
            listOfRecords += Array(nameList.toString)
            csvWriter.writeAll(listOfRecords.toList.asInstanceOf)
            println(&quot;Written!&quot;)
            outputFile.close()
          })
        }
        case JsNull =&gt; println(&quot;Null&quot;)
      }
  </code></pre>
  <p>In the above code on line <strong><code>**csvWriter.writeAll(listOfRecords.toList.asInstanceOf)**</code></strong> I am getting this exception.</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ClassCastException: class scala.collection.immutable.$colon$colon cannot be cast to class scala.runtime.Nothing$ (scala.collection.immutable.$colon$colon and scala.runtime.Nothing$ are in unnamed module of loader 'app')
  </code></pre>
  <p>On removing <em>asInstanceOf</em> from <code>csvWriter.writeAll(listOfRecords.toList.asInstanceOf)</code> this line, I get a compile time error on <em>writeAll()</em> method saying that it expects parameter of type <em>util.List[Array[String]]</em></p>
  <p>Could anyone please help me to solve this problem?</p>