Repository navigation
Default parameter values can't access class scope? #35121
Copy link
Copy link
Open
Labels
status: feedback-reminderWe've sent a reminder that we need additional information before we can continueWe've sent a reminder that we need additional information before we can continuestatus: waiting-for-triageAn issue we've not yet triaged or decided onAn issue we've not yet triaged or decided ontheme: kotlinAn issue related to Kotlin supportAn issue related to Kotlin support
Description
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
- Write a simple spring component (service)
- Set a default for an optional parameter and reference an injected (non-nullable) member property
- 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
status: feedback-reminderWe've sent a reminder that we need additional information before we can continueWe've sent a reminder that we need additional information before we can continuestatus: waiting-for-triageAn issue we've not yet triaged or decided onAn issue we've not yet triaged or decided ontheme: kotlinAn issue related to Kotlin supportAn issue related to Kotlin support
Activity
bclozel commentedon Oct 23, 2026
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.
spyro2000 commentedon Oct 23, 2026
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.
bclozel commentedon Oct 23, 2026
Please share a minimal sample that we can git clone or unzip.