Skip to content

Default parameter values can't access class scope? #35121

Open

Description

@spyro2000

I have a spring service with some injected values. But all injected values are nullwhen accessing them from a default parameter value.

Steps to reproduce

  1. Write a simple spring component (service)
  2. Set a default for an optional parameter and reference an injected (non-nullable) member property
  3. Call this method

Sample:

@Service
class MyService(
  val repo: MyRepository // Injected from Spring
)

fun callUpdate() {
  // `repo` is set when accessed from here
  println("Repo: $repo")
 
  // works
  update(case =  repo.getCase()) // works

 // throws NPE on runtime
  update()
}

fun update(case: CaseBE = repo.getCase()) { // repo is null when accessed from parameter list
  // ...
}

Expected result:

Result of expression is assigned to parameter

Actual result:

Null-pointer exception because all injected properties are null for thiswhen evaluated as default parameter. Seems like the scope of the class is comlpletely lost which is highly unexpected.

It does not even work with an explictly scoped this:

fun update(case: CaseBE = this@MyService.repo.getCase()) { // still NPE when accessing `repo`
  // ...
}

I even tried it with self-reference, same here:

@Service
class MyService(
  val repo: MyRepository // Injected from Spring,
  @Lazy self: MyService, // self-reference as attempted workaround
)

fun callUpdate() {
  // `repo` is set when accessed from here
  println("Repo: $repo")
 
  // works
  update(case =  repo.getCase()) // works

 // throws NPE on runtime
  update()
}

fun upfate(case: CaseBE = self.repo.getCase()) { // repo is null when accessed from parameter list
  // ...
}

I reported that issue to the Kotlin Team (https://youtrack.jetbrains.com/issue/KT-78600) but they immediately redirected me to here because they are convinced that this is an issue with Spring itself or the kotlin-spring plugin.

Thank you.

Activity

  1. bclozel commented on Oct 23, 2026

    @bclozel
    Member

    Sorry for the late feedback. Can you reproduce the problem with our latest maintenance versions? If so, please provide a minimal sample application so we can have a look.

  2. spyro2000 commented on Oct 23, 2026

    @spyro2000
    Author

    Hello and thanks for finally looking into this :)

    Please see sample above or really just try to access an injected component from a default parameter value. That's really everything.

  3. bclozel commented on Oct 23, 2026

    @bclozel
    Member

    Please share a minimal sample that we can git clone or unzip.

  4. added
    status: feedback-reminderWe've sent a reminder that we need additional information before we can continue
    and removed on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: feedback-reminderWe've sent a reminder that we need additional information before we can continuestatus: waiting-for-triageAn issue we've not yet triaged or decided ontheme: kotlinAn issue related to Kotlin support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions