Intellij IDEA java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object

Dawid Mazuruk picture Dawid Mazuruk · Oct 15, 2014 · Viewed 20.5k times · Source

I have a following function:

def removeLast(list: List[Int]): List[Int] = list match {
  case List() => List()
  case List(x) => List()
  case x :: xs => x :: removeLast(xs)
}

When I define it and use it from the sbt console everything works just fine. But when I create a worksheet in Intellij IDEA and try to run it then the following exception appears:

java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object; at week5.A$A26$A$A26.removeLast(lists.sc8362409100671270508.tmp:30) at #worksheet#.#worksheet#(lists.sc8362409100671270508.tmp:33)

In addition, when I change last line to:

case x :: xs => 1 :: removeLast(xs)}

then it works.

What might the problem be?

Answer

Tomek Kozlowski picture Tomek Kozlowski · Oct 16, 2014

I had this issue. Agree with Andrzej, idea uses its own compiler, so you have to disable it somehow. Go to Settings->Scala->Worksheet and uncheck "Run worksheet in the compiler process".