java – 由于null对象引用,无法向RealmList添加新对象

我必须使用realm对象,一个是带有两个字符串和一个布尔值的基本RealmObject,另一个是该对象的RealmList,以便于访问.当我尝试从我的ContactBook实例获取列表并添加新对象时,我尝试在空对象引用上调用虚方法.但是我能够获得联系簿的大小,以及联系人的数量,因此这些不是空的.

类我在哪里尝试添加新对象

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

带有两个字符串的类,以及布尔值

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)

解决方法:

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

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

使用new运算符创建的对象无法在不使用Realm.copyToRealmXXX方法之一的情况下添加到Realm中的RealmLists,因为它必须首先转换为正确(或托管)的Realm对象.但错误信息肯定会更好.

上一篇:Linux安装svn


下一篇:java – Android领域不正确的线程