vicpinm / Kotlin-Realm-Extensions

Kotlin extensions to simplify Realm API.
Apache License 2.0
535 stars 53 forks source link

Attempt to invoke virtual method 'boolean io.realm.RealmObjectSchema.hasPrimaryKey()' on a null object reference #12

Closed norrisboat closed 7 years ago

norrisboat commented 7 years ago

I get this error every time I try to save a record. Please help. This is the code I'm running.

Item("User").save()
SergiyKorotun commented 7 years ago

@norrisboat Does your "Item" object extends RealmObject?

norrisboat commented 7 years ago

@SergiyKorotun yeah.

import io.realm.RealmObject

open class Item() : RealmObject(){

    var name : String = ""

    constructor(name : String) : this() {
        this.name = name
    }
}
SergiyKorotun commented 7 years ago

@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

norrisboat commented 7 years ago

@SergiyKorotun I changed it but getting the same error.

open class Item(@PrimaryKey var name: String) : RealmObject(){
}
SergiyKorotun commented 7 years ago

@norrisboat try to assign default value to your field "name" like in examples

norrisboat commented 7 years ago

@SergiyKorotun I have.

open class Item(@PrimaryKey var name: String="") : RealmObject(){
}

And I'm save it like this.

Item("name").save()
vicpinm commented 7 years ago

@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.

norrisboat commented 7 years ago

ok. Gradle failing to resolve com.github.vicpinm:krealmextensions:1.1.0 but will keep trying

vicpinm commented 7 years ago

It seems to be a problem with the upload process, I'm trying again.

vicpinm commented 7 years ago

@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.