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)
CodePudding user response:
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
CodePudding user response:
App
was never a good idea to begin with, just write a normal main
method.
object HelloYou:
def main(args: Array[String]): Unit =
args.toList match
case Nil =>
println("Hello, you")
case name :: Nil =>
println(s"Hello, ${name}")
case _ =>
println("Hello, you all")
end main
end HelloYou