Uploaded image for project: 'Spring Data Commons'
  1. Spring Data Commons
  2. DATACMNS-1200

Absent parameters cause NPE in ClassGeneratingEntityInstantiator

    XMLWordPrintable

    Details

      Description

      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.

        Attachments

          Activity

            People

            Assignee:
            mp911de Mark Paluch
            Reporter:
            mp911de Mark Paluch
            Last updater:
            Oliver Drotbohm
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: