Skip to content

NullPointerException using abstract class value #9934

Closed
@scabug

Description

@scabug

I have a very weird bug in my code.

I am using Scala 2.11.7 and also tried with 2.11.8.

Problem
I get a NullPointerException when trying to access an abstract class value that contains a default value.

Example

sealed abstract class Language(private val realName: String = "", val extensions: List[String]) {
  self: Product =>

  val name = if(realName.nonEmpty) realName else self.productPrefix
}

case object Scala extends Language(extensions = List(".scala"))

I not always get a NullPointerException, it only happens in the tests if I run all the tests. If I run the test that fails with testOnly it passes.

I was going crazy since yesterday and today I had the idea to change the code to explicitly pass the "default":

sealed abstract class Language(private val realName: Option[String], val extensions: List[String]) {
  self: Product =>

  val name = realName.getOrElse(self.productPrefix)
}

case object Scala extends Language(realName = None, extensions = List(".scala"))

And now it works fine.

Do you have any ideas about what can be going wrong here?

EDIT:

The line 134 is the one with val specificTypeRules ....

object IgnoreManager {
  private[this] lazy val languageIgnoreConfigEnum: Map[String, List[SomeCaseClass]] = ...

  private[this] def languageSpecificRules(ignoreRules: List[ProjectIgnoreRules], language: Language.Language) = {
    val specificTypeRules = languageIgnoreConfigEnum.get(language.name)
    .....
  }
...
}
[info]   java.lang.NullPointerException:
[info]   at rules.project.filerules.IgnoreManager$.languageSpecificRules(IgnoreManager.scala:134)
[info]   at rules.project.filerules.IgnoreManager$.annotateWithIgnored(IgnoreManager.scala:264)
[info]   at rules.project.filerules.IgnoreManagerTests$$anonfun$1.apply$mcV$sp(IgnoreManagerTests.scala:29)
[info]   at rules.project.filerules.IgnoreManagerTests$$anonfun$1.apply(IgnoreManagerTests.scala:28)
[info]   at rules.project.filerules.IgnoreManagerTests$$anonfun$1.apply(IgnoreManagerTests.scala:28)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
[info]   at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:20)
[info]   ...

I just updated the base classes and the rest of the code like it was, before I started changing it (as best as I could). I did lots of testing before understanding the error and I mostly did not commit before I had it fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions