Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6360933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:42:43+00:00 2026-05-24T23:42:43+00:00

If I instantiate Base2 it runs fine class Base2 { class Benchmark { def

  • 0

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)
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:42:44+00:00Added an answer on May 24, 2026 at 11:42 pm

    Base2 is initialized first at which time benchmark.onTrade is executed, however the member benchmark from Base3 (not Base2!) is being used because of the override (benchmark in Base2 is actually initialized first, but it doesn’t matter because the other benchmark is 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.benchmark is still null at benchmark.onTrade.

    FWIW, a “lazy val” seems to “fix” the issue.

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I attempt to instantiate my instance of the base class I get the
I'm using the Activator to instantiate a new class based on the short name
I want to instantiate a base class pointer to point to a newly constructed
Can VBA code instantiate and use .NET objects? The specific class I'm interested in
Let's say I instantiate an object in Javascript like this: var myObj = new
How can I instantiate a class by throwing in a variable name? Consider this
I want to define a base class that defines a main method that instantiates
I usually make a base class abstract to give the signal this is a
An abstract base class (ABC) can have data to support the classes that inherit
I have a base class and several subclasses derived from that base class. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.