Search code examples
javascalascala-3

What's wrong with Hello World in scala?


I am learning scala from docs.scala-lang.org. There is an example

object HelloYou extends App {
    if (args.size == 0)
        println("Hello, you")
    else
        println("Hello, " + args(0))
}

After compiling with scalac I run scala HelloYou Al and get

java.lang.NullPointerException

followed by

    at java.lang.reflect.Array.getLength(Native Method)
    at scala.collection.ArrayOps$.size$extension(ArrayOps.scala:197)
    at HelloYou$.<clinit>(HelloYou.scala:2)
    at HelloYou.main(HelloYou.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

What's wrong? It's my first experience of a language where Hello, World is not working...

Details

scala -version
Scala code runner version 3.1.1 -- Copyright 2002-2022, LAMP/EPFL

java -version
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)

Solution

  • Accessing the command-line arguments like that is no longer supported in Scala 3:

    https://docs.scala-lang.org/scala3/reference/dropped-features/delayed-init.html