Skip to content

NPE when running command line args with extends App #15469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spamegg1 opened this issue Jun 17, 2022 · 4 comments
Closed

NPE when running command line args with extends App #15469

spamegg1 opened this issue Jun 17, 2022 · 4 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@spamegg1
Copy link

spamegg1 commented Jun 17, 2022

Compiler version

3.1.2

Minimized code

Taken from the docs

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

Compile with scalac HelloYou.scala then run with scala HelloYou Spam

Output

Compilation succeeds, but running scala HelloYou Spam crashes:

CLICK TO EXPAND
 ➜ scala HelloYou Spam
java.lang.NullPointerException
	at java.base/java.lang.reflect.Array.getLength(Native Method)
	at scala.collection.ArrayOps$.size$extension(ArrayOps.scala:202)
	at Hello$.<clinit>(Hello.scala:2)
	at Hello.main(Hello.scala)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at dotty.tools.runner.RichClassLoader$.run$extension$$anonfun$1(ScalaClassLoader.scala:36)
	at dotty.tools.runner.ScalaClassLoader$.asContext(ScalaClassLoader.scala:80)
	at dotty.tools.runner.RichClassLoader$.dotty$tools$runner$RichClassLoader$$$asContext$extension(ScalaClassLoader.scala:18)
	at dotty.tools.runner.RichClassLoader$.run$extension(ScalaClassLoader.scala:36)
	at dotty.tools.runner.CommonRunner.run(ObjectRunner.scala:23)
	at dotty.tools.runner.CommonRunner.run$(ObjectRunner.scala:13)
	at dotty.tools.runner.ObjectRunner$.run(ObjectRunner.scala:48)
	at dotty.tools.runner.CommonRunner.runAndCatch(ObjectRunner.scala:30)
	at dotty.tools.runner.CommonRunner.runAndCatch$(ObjectRunner.scala:13)
	at dotty.tools.runner.ObjectRunner$.runAndCatch(ObjectRunner.scala:48)
	at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:212)
	at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:267)
	at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)

Expectation

scala HelloYou Spam should work correctly and print Hello, Spam.

Works as expected when compiled and run with Scala 2.

@spamegg1 spamegg1 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 17, 2022
@bishabosha
Copy link
Member

Thank you for reporting, however this is known to fail: https://docs.scala-lang.org/scala3/reference/dropped-features/delayed-init.html

@bishabosha
Copy link
Member

bishabosha commented Jun 18, 2022

Also, I noticed that the example you used is from the Scala 2 book, the equivalent for Scala 3 is here: https://docs.scala-lang.org/scala3/book/taste-hello-world.html

@spamegg1
Copy link
Author

OK, I also thought that it's probably not a bug, but changed in Scala 3. Weird, it still does compile though! So it's natural to expect it to work.

Just to mention: some newcomers come across these examples in the docs, and they don't know the difference between Scala 2 and 3, or why a piece of code (a Hello world example at that!) straight from the official docs fails. It's not a good look for the language! The docs page starts with Scala 2, and you have to scroll all the way down to see Scala 3.

@bishabosha
Copy link
Member

bishabosha commented Jun 18, 2022

Just to mention: some newcomers come across these examples in the docs, and they don't know the difference between Scala 2 and 3, or why a piece of code (a Hello world example at that!) straight from the official docs fails. It's not a good look for the language! The docs page starts with Scala 2, and you have to scroll all the way down to see Scala 3.

We definitely hear your concerns so hopefully the situation will begin to improve soon with scala/docs.scala-lang#2430

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

2 participants