Sathindu
Sathindu

Reputation: 407

Error while saving Realm object in Android

I'm developing application with Realm database. I had successfully developed that app and today I add another part to that. During that I had to change Object extended RealmObject. After that I got error and I undo that change. But after that also same error happening again and again. Here is my code :

private String createObserver() {
    final Observer observer = new Observer();
    Log.d("UUID",observer.getUuid());
    observer.setObserver_id(getNewObserverID());
    observer.setDevice_id(DEVICE_ID);
    observer.setName(userName.getText().toString());
    observer.setPhone(userPhone.getText().toString());
    observer.setCategory(userCategory.getSelectedItem().toString());
    observer.setHospital(userHospital.getSelectedItem().toString());
    observer.setSection(userSection.getSelectedItem().toString());
    observer.setUpdated_date(DATE);

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.copyToRealmOrUpdate(observer);
        }
    });
    return observer.getUuid();
}

And here's the error I'm getting :

FATAL EXCEPTION: main Process: com.foxastudios.stopnosocomials, PID: 26320 java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.BaseRealm io.realm.ProxyState.getRealm$realm()' on a null object reference at io.realm.ObserverRealmProxy.realmSet$uuid(ObserverRealmProxy.java:239) at com.foxastudios.stopnosocomials.models.Observer.setUuid(Observer.java:107) at com.foxastudios.stopnosocomials.models.Observer.(Observer.java:26) at io.realm.ObserverRealmProxy.(ObserverRealmProxy.java:0) at io.realm.DefaultRealmModuleMediator.newInstance(DefaultRealmModuleMediator.java:145) at io.realm.BaseRealm.get(BaseRealm.java:603) at io.realm.Realm.createObject(Realm.java:709) at io.realm.ObserverRealmProxy.copy(ObserverRealmProxy.java:626) at io.realm.ObserverRealmProxy.copyOrUpdate(ObserverRealmProxy.java:616) at io.realm.DefaultRealmModuleMediator.copyOrUpdate(DefaultRealmModuleMediator.java:175) at io.realm.Realm.copyOrUpdate(Realm.java:1272) at io.realm.Realm.copyToRealmOrUpdate(Realm.java:747) at com.foxastudios.stopnosocomials.SessionActivity$3.execute(SessionActivity.java:143) at io.realm.Realm.executeTransaction(Realm.java:1065) at com.foxastudios.stopnosocomials.SessionActivity.createObserver(SessionActivity.java:140) at com.foxastudios.stopnosocomials.SessionActivity.access$300(SessionActivity.java:29) at com.foxastudios.stopnosocomials.SessionActivity$2.onClick(SessionActivity.java:101) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I have checked several times that any null values in Observer object. But all the values are correct and no exception until :

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        realm.copyToRealmOrUpdate(observer);
    }
});

Upvotes: 1

Views: 1375

Answers (1)

larsgrefer
larsgrefer

Reputation: 2853

The default constructor of Realm objects must be empty:

If declared, the default constructor (constructor with no parameters) must always be empty. The reason is that a default contructor will call methods which assume a Realm instance is present.

Source: https://realm.io/docs/java/latest/#limitations

You are not allowed to call setUuid() in your constructor

Observer.setUuid() will call ObserverRealmProxy.realmGet$uuid() which looks like this:

public int realmGet$uuid() {
    proxyState.getRealm$realm().checkIfValid();
    return (int) proxyState.getRow$realm().getLong(columnInfo.uuidIndex);
}

proxyState is null while the constructor is executed

Upvotes: 2

Related Questions