NPE when create object with realm and kotlin
See original GitHub issueI 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:
- Created 6 years ago
- Comments:14 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
For the record the other issue tracking this is #4372