package test import java.security.Principal import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 data class TestUser(private val _name: String) : Principal { override fun getName(): String = _name override fun toString(): String = _name } data class Column(val property: (TestUser) -> T) abstract class Table { inner class DelegatedColumn(val template: Column) : ReadOnlyProperty> { override fun getValue(thisRef: Any?, property: KProperty<*>): Column = template } fun col(property: KProperty1): DelegatedColumn { val column = Column({ property.get(it) }) return DelegatedColumn(column) } } object testUserTable : Table() { val userName by col(TestUser::name) } fun main(args: Array) { println(testUserTable.userName) } Exception in thread "main" java.lang.ExceptionInInitializerError at test.testUserTable.(TestDao.kt:29) at test.testUserTable.(TestDao.kt:27) at test.TestDaoKt.main(TestDao.kt:33) Caused by: kotlin.TypeCastException: null cannot be cast to non-null type kotlin.reflect.jvm.internal.KDeclarationContainerImpl at kotlin.reflect.jvm.internal.KProperty1Augmented.(KPropertyFromReferenceImpl.kt:42) at kotlin.reflect.jvm.internal.KProperty1FromReferenceImpl.(KPropertyFromReferenceImpl.kt:48) at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.property1(ReflectionFactoryImpl.java:73) at kotlin.jvm.internal.Reflection.property1(Reflection.java:94) at test.testUserTable$userName$2.(TestDao.kt) ... 3 more