Skip to content

Absent parameters cause NPE in ClassGeneratingEntityInstantiator [DATACMNS-1200] #1640

Closed
@spring-projects-issues

Description

@spring-projects-issues

Mark Paluch opened DATACMNS-1200 and commented

Consider following domain objects:

class JavaClass {

	public int param1;

	public ObjectCtor1ParamInt(int param1) {
		this.param1 = param1;
	}
}

data class KotlinClass(val state: Boolean = true)

Instantiating any of these (Java classes with primitives, Kotlin classes with primitives) causes a NullPointerException:

java.lang.NullPointerException
	at org.springframework.data.convert.KotlinClassGeneratingEntityInstantiatorUnitTests$WithDefaultBoolean_Instantiator_480p78.newInstance(Unknown Source)
	at org.springframework.data.convert.KotlinClassGeneratingEntityInstantiator$DefaultingKotlinClassInstantiatorAdapter.createInstance(KotlinClassGeneratingEntityInstantiator.java:240)
	at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:85)
	at org.springframework.data.convert.KotlinClassGeneratingEntityInstantiatorUnitTests.should create instance and fill in default for primitive boolean(KotlinClassGeneratingEntityInstantiatorUnitTests.kt:92)

The NPE is caused from casting null into a primitive. In any case, it's hard to investigate why the issue happens. Ideally, for Java classes, we throw an IAE containing the property name that is null. For Kotlin, we should fall back to a primitive default value and let Kotlin's defaulting set the appropriate value if the parameter has a default.


Affects: 2.0 GA (Kay)

Reference URL: https://github.com/tiarebalbi/spring-data-kotlin-nullable-query/tree/4be37d33caf400f5475c55e8818b90d7eaa583f9

Referenced from: pull request #255

Backported to: 2.0.1 (Kay SR1)

Metadata

Metadata

Assignees

Labels

in: coreIssues in core supporttype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions