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

  • Home
  • SEARCH
  • 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 4556996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:26:44+00:00 2026-05-21T17:26:44+00:00

I have two Scala classes that look like this (paraphrased): abstract class GenericParser[T] {

  • 0

I have two Scala classes that look like this (paraphrased):

abstract class GenericParser[T] {
  val lineFilter : String => Boolean

  parseData()

  def parseData() : T {
    for( line <- .... if lineFilter(line) )
      // do things
  }
}

class SalesParser extends GenericParser[SalesRow] {
  val lineFilter = line => !line.startsWith("//")

  // ....
}

The problem is that lineFilter is null in parseData, presumably because parseData is called while the primary GenericParser constructor is still running, so the subclass hasn’t fully initialized its members.

I can work around this by making lineFilter a def instead of a val, but is this expected behavior? It doesn’t seem right that this problem should only become apparent after getting an NPE at runtime.

  • 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-21T17:26:44+00:00Added an answer on May 21, 2026 at 5:26 pm

    It is indeed the expected behavior, and is exactly the same problem as in this question:

    Scala 2.8: how to initialize child class

    You can basically copy-paste the answer form that question. Solutions include:

    • def or lazy val instead of val
    • early initialization of lineFilter
    • redesign of your classes to avoid the “virtual method call from superclass’s constructor which accesses uninitialized subclass values” problem. For instance, why would you want to store the filter function in a val or return in from a def, while it could be implemented as a method?

      abstract class GenericParser[T] {
        def lineFilter(line: String): Boolean
      
        parseData()
      
        def parseData() : T {
          for( line <- .... if lineFilter(line) )
            // do things
        }
      }
      
      class SalesParser extends GenericParser[SalesRow] {
        def lineFilter(line: String) = !line.startsWith("//")
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two packrat parsers in scala: val symbols : PackratParser[String] = { |
I have two classes, and want to include a static instance of one class
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
I have two Java class hierarchies that share a common ancestor and implement a
first of all, i like to say that i have read the other two
I have those two classes ///////////BASE CLASS class Base{ public: Base(int = 0); ~Base();
I have a base class, Parameter, and two derived classes: Scalar & Vector. In
I have two classes, a server and some clients. The server class make a
I have two applications written in Java that communicate with each other using XML
Suppose I have two lists: val a = List('a', 'b', 'c') val b =

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.