title: Scala List to Java cannot be cast
body: <p>I've made two sealed abstract classes in Scala</p>
  <pre class="lang-scala prettyprint-override"><code>sealed abstract class PixelList
  case class PCons(hd: (Int, Int), tl: PixelList) extends PixelList
  case class PNil() extends PixelList
  
  sealed abstract class DrawingList
  case class DCons(hd: (String, PixelList), tl: DrawingList) extends DrawingList
  case class DNil() extends DrawingList
  </code></pre>
  <p>In my java I get their results in this function:</p>
  <pre class="lang-java prettyprint-override"><code>//Scala getting an array and returning the list
  drawEngine.DrawingList drawingList = newDraw.parseCommands(lines);
  
  DrawPixels(drawingList);
  
  private static void DrawPixels(drawEngine.DrawingList lists){
      System.out.println(lists);
      drawEngine.DCons cons = (drawEngine.DCons) lists;
      System.out.println(cons);
      drawEngine.PCons pCons = (drawEngine.PCons) cons.hd()._2;
      System.out.println(pCons);
  
      while (pCons instanceof drawEngine.PCons) {
          drawEngine.PCons pCon = (drawEngine.PCons) pCons;
          System.out.println(pCon.hd());
          pCons = (drawEngine.PCons) pCon.tl();
      }
  }
  </code></pre>
  <p>The print output and error is the following:</p>
  <p><a href="https://i.stack.imgur.com/1rf4h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1rf4h.png" alt="enter image description here" /></a></p>
  <p>How can it be that my <code>While loop creates this error? --&gt;  drawEngine$PNil cannot be cast to drawEngine$PCons</code></p>
  <p>Some of the Scala code:
  <code>parseCommands</code>, takes the array from java, <code>[drawline(0, 0, 2, 2)]</code>, and takes it's values, and parse them on to the other functions.</p>
  <p><code>drawCommands</code> looks at what case this data fits in, and appends the values to drawObject which is returned.</p>
  <pre class="lang-scala prettyprint-override"><code>def parseCommands(inpuCmd:Array[String]): DrawingList = {
    println(&quot;SCALA &quot; + inpuCmd.mkString(&quot; &quot;) );
  
    // Format input
    val formatCmd = for(row &lt;- inpuCmd) yield {
      val res = row
        .toLowerCase()
        .replaceAll(&quot;\\s+&quot;,&quot;&quot;)
        .replace(&quot;(&quot;, &quot;,&quot;)
        .replace(&quot;)&quot;, &quot;&quot;)
        .split(&quot;,&quot;)
      res
    }
    println(formatCmd(0).mkString(&quot; &quot;) );
    // formatCmd(0)[cmd, arg1,arg....]
  
  
  
    val drawing = drawCommands(formatCmd);
    //val drawing = drawCommands(cmdOnly, argsOnly.toArray);
  
    //return true;
    return drawing;
  }
  
  
  def drawCommands(inCmd:Array[Array[String]]) : DrawingList = {
    println(&quot;drawCommands&quot;);
  
    var drawObject:DrawingList = DNil();
    var tempDraw:DrawingList = drawObject;
  
    val drawList = for(row &lt;- inCmd) yield {
      tempDraw = drawObject
  
      if(row(0) != null){
        println(&quot;\n row(0) --&gt; &quot; + row(0));
        row(0) match {
          case &quot;bounding-box&quot; =&gt; drawObject = Append(tempDraw, (&quot;BLACK&quot;, drawRectangle( row(1).toInt, row(2).toInt, row(3).toInt, row(4).toInt )))
          case &quot;drawcircle&quot; =&gt;  drawObject = Append(tempDraw, (&quot;BLACK&quot;, drawCircle( row(1).toInt, row(2).toInt, row(3).toInt )))
          case &quot;drawline&quot; =&gt;  drawObject = Append(tempDraw, (&quot;BLACK&quot;, drawLine( row(1).toInt, row(2).toInt, row(3).toInt, row(4).toInt )))
          case &quot;drawrectangle&quot; =&gt; drawObject = Append(tempDraw, (&quot;BLACK&quot;, drawRectangle( row(1).toInt, row(2).toInt, row(3).toInt, row(4).toInt )))
          case _ =&gt; throw new Exception(&quot;Unknown command: &quot; + row )
        }
      }
    }
    //println(drawObject);
  
    return drawObject;
  }
  
  def Append(dList: DrawingList, item: (String, PixelList)): DrawingList = dList match{
    case DNil() =&gt; DCons(item, DNil())
    case DCons(head, tail) =&gt; DCons(head, Append(tail, item))
  }
  </code></pre>