Error: type mismatch; found : Array[java.lang.Double] required: Array[scala.Double]

I have defined this function:

  def getArrayChannelData(row: IndexedChannelRow): Array[ChannelArrayRow] = {
    // We ignore the array if readoutValue is not set
    val value: Array[java.lang.Double] = row.readoutValue match {
      case Some(readoutValue) => try { readoutValue.getConvertedArrayValues match {
        // The array is expanded into rows
        case array: Array[java.lang.Double] => array
            val offset: Int = try { row.channel.getOffset } catch { case _:Throwable => 0 }
            val hashId = "asdf"
            val rowIndex = 123
>             ChannelArrayRow(hashId, row.docId, getChannelId(row), offset, System.currentTimeMillis, rowIndex, Some(value))
>             
>         // The object is not an array
>         case _ => Array()
>       }} catch { case _:Throwable => Array() }
>       case None => Array()
>     }
>   }

With:

case class ChannelArrayRow(
hashId: String,
docId: Long,
channelId: String,
offset: Int,
created: Long,
index: Int,
value: Option[Array[Double]]
)

And i recive this error, "rror: type mismatch;

found : Array[java.lang.Double]
required: Array[scala.Double]
ChannelArrayRow(hashId, row.docId, getChannelId(row), offset, System.currentTimeMillis, rowIndex, Some(value))"

As the error says, your problem is that your array uses java.lang.Double, while your case class expects a scala.Double. The java.lang.Double is the “boxed” version, i.e. it has a wrapper to act as a reference type. In Scala code, you usually shoudn’t need it, and even in Java an Array can work with the unboxed, primitive double. So check the return type of your readoutValue.getConvertedArrayValues. If it is Array[Double] (or if it is from Java source: double[]), just remove java.lang. before any Double.

If getConvertedArrayValues actually gives you the boxed doubles, i.e. Array[java.lang.Double] in Scala or Double[] (note the uppercase) in Java, you have two options:

  1. Unbox the doubles before storing them in your case class, e.g. with value.map(_.toDouble).
  2. Change your case class to use a java.lang.Double array