Cant adds new object to RealmList due to null object reference

I have realm objects, one of which is a basic RealmObject with two strings and a boolean, and the other is just a RealmList for that object for easier access. When I try to get a list from my ContactBook instance and add a new object, I get an attempt to call a virtual method with a null object reference. However, I can get the size of the contact book and the number of contacts so that they are not null.

Class Where I am trying 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;
}
}

      

Contact book class

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

      

Error log

    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)

      

+3


source to share


1 answer


This is because you are trying to add a non-managed object to a managed list. This code is 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));
}

      

it should be:



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

      

Objects created with an operator new

cannot be added to RealmLists that are already in Realm without using one of the methods Realm.copyToRealmXXX

, because it must first be converted to a valid (or managed) Realm object. But the error message could be better.

+10


source







All Articles