Reputation: 313
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;
}
}
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)
Upvotes: 3
Views: 8438
Reputation: 20126
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));
}
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 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.
Upvotes: 10