If I instantiate Base2 it runs fine
class Base2 {
class Benchmark {
def onTrade() {
println("base onTrade")
}
}
protected val benchmark = new Benchmark
benchmark.onTrade
}
class Base3 extends Base2 {
override val benchmark = new Benchmark {
override def onTrade() {
println("sub onTrade")
}
}
}
// to run
new Base3
Exception info:
java.lang.NullPointerException
at Base2.<init>(<console>:16)
at Base3.<init>(<console>:19)
at .<init>(<console>:10)
at .<clinit>(<console>)
at .<init>(<console>:11)
...
Base2 is initialized first at which time
benchmark.onTradeis executed, however the memberbenchmarkfrom Base3 (not Base2!) is being used because of the override (benchmarkin Base2 is actually initialized first, but it doesn’t matter because the otherbenchmarkis being “bound” to!). I am fairly certain this has to do with the val being virtual…Base3’s initialization won’t start (or resume?) until after Base2’s is finished and thus
Base3.benchmarkis still null atbenchmark.onTrade.FWIW, a “lazy val” seems to “fix” the issue.
Happy coding.