Closed norrisboat closed 7 years ago
@norrisboat Does your "Item" object extends RealmObject?
@SergiyKorotun yeah.
import io.realm.RealmObject
open class Item() : RealmObject(){
var name : String = ""
constructor(name : String) : this() {
this.name = name
}
}
@norrisboat The problem is in your Item class, try to remove constructor.
e.g.
open class Item(var name: String = "") : RealmObject()
see example in realm repo https://github.com/realm/realm-java/blob/master/examples/kotlinExample/src/main/kotlin/io/realm/examples/kotlin/model/Person.kt
@SergiyKorotun I changed it but getting the same error.
open class Item(@PrimaryKey var name: String) : RealmObject(){
}
@norrisboat try to assign default value to your field "name" like in examples
@SergiyKorotun I have.
open class Item(@PrimaryKey var name: String="") : RealmObject(){
}
And I'm save it like this.
Item("name").save()
@norrisboat Sorry but I cant reproduce your problem, its working fine in my project. I think is a issue related with realm plugin. I have uploaded a new version to maven (1.1.0) with the lastest realm version (3.5.0) and kotlin compiler 1.1.3. You can try with that version.
ok. Gradle failing to resolve com.github.vicpinm:krealmextensions:1.1.0 but will keep trying
It seems to be a problem with the upload process, I'm trying again.
@norrisboat I think your problem comes from a bad configuration in your build.gradle. Do this test: replace your Item("name").save() method with a normal realm copy operation (realm.executeInTransaction{ it.copyToRealm(Item("name")) }. I think you will have an error like "Item is not part of the schema for this Realm". It means that realm plugin is not executed in the build process, and that entity is not registered. Check if you have the realm plugin line added to your build.gradle in the app module:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'
The NullPointerException you are getting comes from this line in the library:
private fun <T : RealmObject> T.hasPrimaryKey(realm : Realm) = realm.schema.get(this.javaClass.simpleName).hasPrimaryKey()
realm.schema.get(this.javaClass.simpleName) returns null, which means that your Item entity is not added to realm in the build process. I will check that scenario in the next version in order to clarify this problem.
I get this error every time I try to save a record. Please help. This is the code I'm running.