在Android

时间:2016-07-02 15:44:44

标签: android realm

我正在使用Realm数据库开发应用程序。我已成功开发了该应用程序,今天我又添加了另一部分。在此期间,我不得不更改Object extended RealmObject。之后我得到了错误,我撤消了这个改变。但在那之后也一次又一次地发生同样的错误。这是我的代码:

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();
}

这就是我得到的错误:

  

致命的例外:主要                                                                                    过程:com.foxastudios.stopnosocomials,PID:26320                                                                                    java.lang.NullPointerException:尝试调用虚方法   ' io.realm.BaseRealm io.realm.ProxyState.getRealm $ realm()'在null   对象参考                                                                                        在   io.realm.ObserverRealmProxy.realmSet $ UUID(ObserverRealmProxy.java:239)                                                                                        在   com.foxastudios.stopnosocomials.models.Observer.setUuid(Observer.java:107)                                                                                        在   com.foxastudios.stopnosocomials.models.Observer。(Observer.java:26)                                                                                        在io.realm.ObserverRealmProxy。(ObserverRealmProxy.java:0)                                                                                        在   io.realm.DefaultRealmModuleMediator.newInstance(DefaultRealmModuleMediator.java:145)                                                                                        在io.realm.BaseRealm.get(BaseRealm.java:603)                                                                                        at io.realm.Realm.createObject(Realm.java:709)                                                                                        at io.realm.ObserverRealmProxy.copy(ObserverRealmProxy.java:626)                                                                                        在   io.realm.ObserverRealmProxy.copyOrUpdate(ObserverRealmProxy.java:616)                                                                                        在   io.realm.DefaultRealmModuleMediator.copyOrUpdate(DefaultRealmModuleMediator.java:175)                                                                                        at io.realm.Realm.copyOrUpdate(Realm.java:1272)                                                                                        at io.realm.Realm.copyToRealmOrUpdate(Realm.java:747)                                                                                        在   com.foxastudios.stopnosocomials.SessionActivity $ 3.execute(SessionActivity.java:143)                                                                                        在io.realm.Realm.executeTransaction(Realm.java:1065)                                                                                        在   com.foxastudios.stopnosocomials.SessionActivity.createObserver(SessionActivity.java:140)                                                                                        在   com.foxastudios.stopnosocomials.SessionActivity.access $ 300(SessionActivity.java:29)                                                                                        在   com.foxastudios.stopnosocomials.SessionActivity $ 2.onClick(SessionActivity.java:101)                                                                                        在android.view.View.performClick(View.java:4756)                                                                                        在android.view.View $ PerformClick.run(View.java:19749)                                                                                        在android.os.Handler.handleCallback(Handler.java:739)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                        在android.os.Looper.loop(Looper.java:135)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5221)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我已经多次检查过Observer对象中的任何空值。但是所有这些值都是正确的,直到下列情况都没有例外:

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

1 个答案:

答案 0 :(得分:2)

Realm对象的默认构造函数必须为空:

  

如果声明,默认构造函数(没有参数的构造函数)必须始终为空。原因是默认的构造函数将调用假定存在Realm实例的方法。

来源:https://realm.io/docs/java/latest/#limitations

您不能在构造函数中调用setUuid()

Observer.setUuid()会调用ObserverRealmProxy.realmGet$uuid(),如下所示:

public int realmGet$uuid() {
    proxyState.getRealm$realm().checkIfValid();
    return (int) proxyState.getRow$realm().getLong(columnInfo.uuidIndex);
}
执行构造函数时

proxyStatenull