Skip to content Skip to sidebar Skip to footer

Setting A Realmobject To Another Realmobject After Retrieving It From Realm

I am having an issue with Realm which is causing it to crash with a NullPointerException everytime I try and set a RealmObject to another after it's been stored. Eg. Person pe

Solution 1:

Pet = new Pet() will create a standalone Object which is not managed by Realm yet. And it is the reason personFromRealm.setPet(pet) crash. However, the error message here is not user friendly at all...

Try:

Petpet=newPet();
pet.setType("dog");
pet = realm.copyToRealm(pet);
personFromRealm.setPet(pet);

or simpler:

Pet pet = realm.createObject(Pet.class);
pet.setType("dog");
personFromRealm.setPet(pet);

Both of them need to be in a transaction.

https://github.com/realm/realm-java/issues/1558 is created for a better exception message.

Post a Comment for "Setting A Realmobject To Another Realmobject After Retrieving It From Realm"