Delegated fields throw NPE


#1

Hello I want to report a bug. I have an hibernate entity that is returning NPE for the delegated fields.

---- BaseUser.kt ----

  @Entity
  @Table("base_user")
  @Inheritance(strategy = JOINED)
  abstract class BaseUser(
    id: Long,

    @OneToOne
    @JoinColumn(name = "base_user_id", referencedColumnName = "id", nullable = false, unique = true)
    private val user User
) BaseModel(id) { val email: String by user }

---- User.kt ----

  @Entity
  @Table("user")
  data class User(
      @Id val id: Long,
      var email: String
  ) {
      operator fun <T> getValue(user: User, property: KProperty<*>) : T {
          return User::class.declaredMemberProperties.first { it.name == property.name }.get(this) as T
      }
  }

When I get this entity from the DB the email is null and the email$delegate throws NPE. Checking the decompiled java code I see that it has a non-args constructor which is not setting the delegate behaviour.

---- BaseUser.decompiled.java ----

  public BaseUser(long id, @NotNull User user) {
    Intrinsics.checkParameterIsNotNull(user, "user");
    super(id);
    this.user = user;
    this.email$delegate = this.user;
  }

  public BaseUser() {
  }

#2

Hello. Thank you for the report. Unfortunately I couldn’t build a reproducible test case from the code that you provided. Could you please create an issue at http://kotl.in/issue with a sample project that reproduces the bug? Thanks!