question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NPE when create object with realm and kotlin

See original GitHub issue

I try to use realm work with kotlin but it crash when I create object with the below NPE.

java.lang.NullPointerException: Attempt to invoke virtual method ‘void io.realm.ProxyState.setConstructionFinished()’ on a null object reference

Code Sample


Chat.kt

open class Chat(@PrimaryKey open var chatId: Long = -1) : RealmObject()

Fragment.kt

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        notification_recycler_view.layoutManager = LinearLayoutManager(appCtx)
        val realm = Realm.getDefaultInstance()
        val data = realm.where(Chat::class.java).findAll()
        notification_recycler_view.adapter = NotificationAdapter(data!!)

        realm.beginTransaction()
        val chat = realm.createObject(Chat::class.java, 1)
        realm.commitTransaction()
    }

Version of Realm and tooling

Realm version(s): ?

io.realm:realm-gradle-plugin:3.1.3

io.realm:android-adapters:2.0.0

Realm sync feature enabled: yes/no

no

Android Studio version: ?

2.3.1

Which Android version and device: ?

Android 6.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
akentcommented, May 10, 2017

I have also started seeing this issue on my project. If I make any code change in a RealmObject derived class, it triggers it. A clean build does fix it. I’ll try to repro with the demo project linked above.

0reactions
akentcommented, May 11, 2017

For the record the other issue tracking this is #4372

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - null pointer exception error while mentioning realm query
NullPointerException : Attempt to invoke virtual method >'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object ...
Read more >
Define a Realm Object Schema - Java SDK - MongoDB
Realm objects created by implementing RealmModel can access those same methods statically through the RealmObject class: Kotlin. Java ...
Read more >
Say Bye to NullPointerException in Kotlin - Medium
Using Null Object pattern in our projects. Singleton pattern: the Singleton pattern confirms that just only one instance of a class is created....
Read more >
Returning an Object from a Realm query throws instance ...
Apart from client coding help, what is the benefit of '!!' operator in Kotlin if it throws NPE when the object is null?...
Read more >
Two Birds with One Stone - Realm Academy
This article examines the adoption rate of Kotlin on Android and tips ... To define objects public class Dog extends RealmObject { private ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found