question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initialisation Exception

See original GitHub issue

Compiler version

3.1.3 and 3.2.0-RC1 on Scastie

Minimized code

object A {
  class Builder[K, V](creator: K => V) {
    def build(k: K): V = creator(k)
  }
  case class Foo(id: Int) extends AnyVal
  object Foo extends Builder[Int, Foo](k => new Foo(k)) {
    def apply(str: String): Foo = str match {
      case "Foo1" => foo1
    }
  }
  val foo1: Foo = Foo.build(1)
}

object B {
  val foo = A.Foo.apply("Foo1")
}

B.foo

Output

java.lang.ExceptionInInitializerError
	at Playground$A$Foo$.<init>(main.scala:8)
	at Playground$A$Foo$.<clinit>(main.scala:8)
	at Playground$B$.<clinit>(main.scala:17)
	at Playground$.<clinit>(main.scala:20)
	at Main$.<clinit>(main.scala:24)
	at Main.main(main.scala)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at sbt.Run.invokeMain(Run.scala:143)
	at sbt.Run.execute$1(Run.scala:93)
	at sbt.Run.$anonfun$runWithLoader$5(Run.scala:120)
	at sbt.Run$.executeSuccess(Run.scala:186)
	at sbt.Run.runWithLoader(Run.scala:120)
	at sbt.Run.run(Run.scala:127)
	at com.olegych.scastie.sbtscastie.SbtScastiePlugin$$anon$1.$anonfun$run$1(SbtScastiePlugin.scala:38)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:17)
	at sbt.ScastieTrapExit$App.run(ScastieTrapExit.scala:259)
	at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "Playground$A$Foo$.build(Object)" because "Playground$A$Foo$.MODULE$" is null
	at Playground$A$.<clinit>(main.scala:13)
	... 21 more

Expectation

My initialisation ordering might be dubious but this works in Scala 2.

Works if I change val foo1 to lazy val foo1 or if I put all the code intoobject A

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
som-snyttcommented, Aug 8, 2022

@Arthurm1 I’ve never actually used this before, but:

➜  scalac -Ysafe-init i15832.scala
-- Warning: i15832.scala:4:32 ------------------------------------------------------------------------------------------
4 |    def build(k: K): V = creator(k)
  |                         ^^^^^^^^^^
  |               Call method Builder.this.creator.apply(k) on a value with an unknown initialization. Calling trace:
  |               -> val foo1: Foo = Foo.build(1)       [ i15832.scala:12 ]
  |                                  ^^^^^^^^^^^^
1 warning found

Edit: I tried it with HEAD but no warning, although it fails the same way.

0reactions
liufengyuncommented, Aug 8, 2022

Edit: I tried it with HEAD but no warning, although it fails the same way.

Thanks for looking into it @som-snytt . The behavior of the HEAD is expected, it’s due to a recent improvement #15467.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java: Appropriate exception for initialization error
If you are throwing an exception in a Factory due to insufficient data, I like to throw an IllegalStateException with a description similar ......
Read more >
How to Handle the Exception In Initializer Runtime Error in Java
Java's ExceptionInInitializerError occurs when an unchecked exception takes place in a static initializer or static variable assignment.
Read more >
InitializationException (Content Assembler API 2.1.2)
Represents an unrecoverable exception while initializing a content assembler -- usually during the construction of a ContentManager .
Read more >
When Does Java Throw the ExceptionInInitializerError?
The ExceptionInInitializerError indicates that an unexpected exception has occurred in a static initializer. Basically, when we see this ...
Read more >
LazyInitializationException - What it is and the best way to fix it
Hibernate throws the LazyInitializationException when it needs to initialize a lazily fetched association to another entity without an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found