领域中的空指针异常

时间:2015-02-28 11:10:14

标签: java android realm

我有一些功能,可以在realm DB中保存数据'Dialog'和'Message'。

private void addMessageToDialog(ObservableDialog dialog, DialogMessage dialog_msg) {
        // if i try to use FINDDIALOG - i have NPE errors
        rDialog rDialog  = findDialog(dialog.getChatId());
        realm.beginTransaction();
        rMessage rMessage = constructObjectRMessage(dialog_msg);
        realm.copyToRealm(rMessage);            
        if(rDialog==null) rDialog = constructObjectRDialog(dialog);
        rDialog.getDialogMessages().add(rMessage);
        realm.copyToRealmOrUpdate(rDialog);
        realm.commitTransaction();
    }
private rDialog constructObjectRDialog(ObservableDialog dialog) {
        try {
            rDialog rDialog = new rDialog();
            rDialog.setChatId(dialog.getChatId());
            rDialog.setCreator(dialog.isCreator());
            rDialog.setNickname(dialog.getNickname());
            rDialog.setAvatarUrl(dialog.getAvatarUrl());
            rDialog.setPartnerOkId(dialog.getPartnerId());
            rDialog.setBanned(dialog.isBanned());

            List<ChatStatus> statusHistory = dialog.getStatusHistory();
            RealmList<rChatStatus> statuses = new RealmList<>();
            for (ChatStatus s : statusHistory) {
                rChatStatus st = constuctorRChatCtatus(s.name());
                realm.copyToRealm(st);
                statuses.add(st);
            }
            rDialog.setStatusHistory(statuses);

            List<DialogMessage> dialogMessages = dialog.getMessages();
            RealmList<rMessage> messages = new RealmList<>();
            for (DialogMessage d : dialogMessages) {
                rMessage m = constructObjectRMessage(d);
                realm.copyToRealm(m);
                messages.add(m);
            }
            rDialog.setDialogMessages(messages);
            return rDialog;
        } catch(Exception e){
            Log.e(TAG,e.getMessage());
        }
        return null;
    }


private rDialog findDialog(String chatId) {
        RealmResults<rDialog> d = realm.where(rDialog.class)
            .equalTo("chatId", chatId)
            .findAll();             
        if(d.size()==0) return null;
        return d.first();
    }

当我使用此代码时,仅当我不使用findDialog(和realm.where方法,并且仅使用constructObjectRDialog)时,它才有效。 但是当我尝试使用findDialog(在addMessageToDialog中)时,我在

中有NPE错误
 rDialog.getDialogMessages().add(rMessage);

为什么会出现这样的错误? PS - rMessagerDialog不为空(当我在调试器中检查时)

LOG:

02-28 11:43:21.220    6432-6460/com.OkLove.OkLoveDroid E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-468
    Process: com.OkLove.OkLoveDroid, PID: 6432
    java.lang.NullPointerException
            at io.realm.RealmList.add(RealmList.java:120)
            at com.OkLove.OkLoveDroid.BackgroundTasksHandlers.DbHandler.addMessageToDialog(DbHandler.java:98)
            at com.OkLove.OkLoveDroid.BackgroundTasksHandlers.DbHandler.handleMessage(DbHandler.java:77)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at com.OkLove.OkLoveDroid.BackgroundTasksHandlers.DbHandlerThread.run(DbHandlerThread.java:30)

1 个答案:

答案 0 :(得分:2)

看来,问题出现在'具有Realm类型的对象的副本'和'realm db object的真实实例'中,请参阅https://groups.google.com/forum/#!topic/realm-java/lK6Q6zRYTE0

我编辑的代码现在看起来像:

 private void addMessageToDialog(ObservableDialog dialog, DialogMessage dialog_msg) {
        rDialog rDialog  = findDialog(dialog.getChatId());
        rMessage rMessage = constructObjectRMessage(dialog_msg);
        realm.beginTransaction();
        rMessage = realm.copyToRealm(rMessage);
        if(rDialog==null) {
            rDialog = constructObjectRDialog(dialog);
            if(rDialog!=null) rDialog = realm.copyToRealm(rDialog);
            else realm.cancelTransaction();
        } else Log.v(TAG,"dialog is not null");
        rDialog.getDialogMessages().add(rMessage);
        realm.commitTransaction();
    }

执行copyToRealm时 - 应该使用返回类型rDialog来编辑域对象(但不能复制独立对象)