由于对象引用为空,因此无法将新对象添加到RealmList中 [英] Cant add new object to RealmList due to null object reference

查看:66
本文介绍了由于对象引用为空,因此无法将新对象添加到RealmList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对对象进行领域处理,一个是具有两个字符串和一个布尔值的基本RealmObject,另一个是该对象的RealmList,以便于访问.当我尝试从我的ContactBook实例中获取列表并添加一个新对象时,我尝试在空对象引用上调用虚拟方法.但是,我无法获取contactBook的大小以及联系人的数量,因此这些联系人不为空.

I have to realm objects, one is a basic RealmObject with a two strings and a boolean, the other is just a RealmList for that object for easier access. When I try to get the list from my instance of the ContactBook and add a new object I get the attempt to invoke virtual method on a null object reference. However Im able to get the size of the contactBook, and the amount of contacts so those are not null.

我尝试在其中添加新对象的类

Class Where I try to add new objects

public void viewSetup() {
    setContentView(R.layout.activity_contacts);
    ButterKnife.inject(this);
    Realm.deleteRealmFile(this);
    realm = Realm.getInstance(this);
    results = realm.where(ContactBook.class).findAll();
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }

    if (results.size() == 0) {
        realm.beginTransaction();
        ContactBook contactBook = realm.copyToRealm(new ContactBook());
        realm.commitTransaction();
    }
}

public void setupCursor() {
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    ContactBook contactBook = results.first();

    realm.beginTransaction();
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        if (contactBook.getContacts().where().equalTo("name", name).findAll().size() == 0 && contactBook.getContacts().where().equalTo("number", number).findAll().size() == 0) {
            contactBook.getContacts().add(new Contact(name, number, false));
        }
    }
    realm.commitTransaction();



    cursor.close();
}

带有两个字符串和布尔值的类

class with two strings, and boolean

public class Contact extends RealmObject {

private String name;
private String number;
private boolean allowed;

public Contact() {

}

public Contact(String name, String number, boolean allowed) {
    this.name = name;
    this.number = number;
    this.allowed = allowed;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

public boolean isAllowed() {
    return allowed;
}

public void setAllowed(boolean allowed) {
    this.allowed = allowed;
}
}

联系方式课程

public class ContactBook extends RealmObject {

private RealmList<Contact> contacts = new RealmList<>();

public ContactBook() {

}

public RealmList<Contact> getContacts() {
    return contacts;
}

public void setContacts(RealmList<Contact> contactBook) {
    this.contacts = contactBook;
}
}

错误日志

    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.github.brady131313.textback/io.github.brady131313.textback.View.ActivityContactSelect}: java.lang.NullPointerException: Attempt to invoke virtual method 'long io.realm.internal.Row.getIndex()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2661)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.access$900(ActivityThread.java:172)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5835)
        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:1388)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long io.realm.internal.Row.getIndex()' on a null object reference
        at io.realm.RealmList.add(RealmList.java:122)
        at io.github.brady131313.textback.View.ActivityContactSelect.setupCursor(ActivityContactSelect.java:69)
        at io.github.brady131313.textback.View.ActivityContactSelect.onCreate(ActivityContactSelect.java:39)
        at android.app.Activity.performCreate(Activity.java:6221)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5835)
            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:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

推荐答案

这是因为您试图将非托管对象添加到托管列表中.这段代码在这里:

It is because you are trying to add a non-managed object to a managed list. This code here:

if (contactBook.getContacts().where().equalTo("name", name).findAll().size() == 0 && contactBook.getContacts().where().equalTo("number", number).findAll().size() == 0) {
    contactBook.getContacts().add(new Contact(name, number, false));
}

应该是:

if (contactBook.getContacts().where().equalTo("name", name).findAll().size() == 0 && contactBook.getContacts().where().equalTo("number", number).findAll().size() == 0) {
    Contact newContact = realm.copyToRealm(new Contact(name, number, false));
    contactBook.getContacts().add(newContact);
}

在不使用Realm.copyToRealmXXX方法之一的情况下,不能将使用new运算符创建的

对象添加到已存在于Realm中的RealmLists中,因为必须先将其转换为适当的(或托管的)Realm对象.但是错误消息肯定会更好.

Objects created with the new operator cannot be added to RealmLists that are already in the Realm without using one of the Realm.copyToRealmXXX methods, because it has to be converted to a proper (or managed) Realm object first. But the error message could definitely be better.

这篇关于由于对象引用为空,因此无法将新对象添加到RealmList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆