I have added a new field (lastUpdate
) to an Object stored in Realm DB:
public class ChartObject extends RealmObject {
...
private Long lastUpdate = 0L;
public ChartObject() {
...
lastUpdate = 0L;
}
...
public long getLastUpdate() { return lastUpdate; }
public void setLastUpdate(long lastUpdate) { this.lastUpdate = lastUpdate;
}
I have added it to MyMigration.java:
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion < 4) {
schema.get("ChartObject")
.addField("lastUpdate", Long.class); // how to set default value?
oldVersion++;
}
}
Now when I am trying to access it via getLastUpdate()
I get
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
How do I properly set the default value?
If you have pre-existing objects, then you need to update them in the transform.
if (oldVersion < 4) {
schema.get("ChartObject")
.addField("lastUpdate", Long.class);
schema.get("ChartObject").transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setLong("lastUpdate", 0L);
}
});
oldVersion++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With