将域模型与ItemViewModel中的可空字段绑定

时间:2017-11-26 11:30:32

标签: kotlin tornadofx

运行此代码时:

class PersonApp : App(PersonView::class)
class Person {
    var name: String? = null
}
class PersonModel: ItemViewModel<Person>() {
    val name = bind(Person::name)
}
class PersonView : View() {
    val model: PersonModel by inject()
    override val root = form {
        textfield(model.name)
    }
}

抛出以下异常:

Nov 26, 2017 12:18:10 PM tornadofx.DefaultErrorHandler uncaughtException
SEVERE: Uncaught error
kotlin.TypeCastException: null cannot be cast to non-null type javafx.beans.property.Property<N>
    at favetelinguis.bfgx.PersonModel$$special$$inlined$bindMutableNullableField$1.invoke(ViewModel.kt:538)
    at favetelinguis.bfgx.PersonModel$$special$$inlined$bindMutableNullableField$1.invoke(ViewModel.kt:512)
    at favetelinguis.bfgx.PersonModel.<init>(Exeee.kt:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at tornadofx.FXKt.find(FX.kt:413)
    at favetelinguis.bfgx.Bree$$special$$inlined$inject$1.getValue(Component.kt:954)
    at favetelinguis.bfgx.Bree$$special$$inlined$inject$1.getValue(Component.kt:151)
    at favetelinguis.bfgx.Bree.getModel(Exeee.kt)
    at favetelinguis.bfgx.Bree$root$1.invoke(Exeee.kt:20)
    at favetelinguis.bfgx.Bree$root$1.invoke(Exeee.kt:17)
    at tornadofx.FXKt.opcr(FX.kt:454)
    at tornadofx.FormsKt.form(Forms.kt:23)
    at favetelinguis.bfgx.Bree.<init>(Exeee.kt:19)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at tornadofx.FXKt.find(FX.kt:413)
    at tornadofx.FXKt.find$default(FX.kt:398)
    at tornadofx.App.start(App.kt:79)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

使用域模型的惯用方法是什么,其中所有初始值都为null并且要从GUI设置?这就是我如何更改上面的代码以使其按预期工作?

0 个答案:

没有答案