I have two objects: MailBox
and Email
. Each Receiver has many Emails
.
public class MailBoxRealmModel extends RealmObject {
@PrimaryKey
private long id;
private String name;
private String mailboxId;
private RealmList<EmailRealmModel> emails;
}
public class EmailRealmModel extends RealmObject {
@PrimaryKey
private long EmailId;
private String Name;
private String Text;
private String Tag;
private int Type;
private String Time;
private int Status;
}
How can one use **realm.insertOrUpdate**
when adding email to MailBoxRealmModel?
EmailRealmModel email = new EmailRealmModel();
email.setMessageId(emailID);
realm.insertOrUpdate(email );
mailBoxRealmModel.getEmails().add(email);
I am getting a null pointer exception email
on this line:
mailBoxRealmModel.getEmails().add(email);
Exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.realm.RealmList.add(io.realm.RealmModel)' on a null object reference at com.koa.mailbox.MailBoxActivity$1.execute(MailBoxActivity.java:123) at io.realm.Realm.executeTransaction(Realm.java:1253) at com.koa.mailbox.MailBoxActivity.test(MailBoxActivity.java:88) at com.koa.mailbox.MailBoxActivity.onCreate(MailBoxActivity.java:71) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) at android.app.ActivityThread.access$1100(ActivityThread.java:222) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method)
On an unmanaged RealmObject, the
RealmList
fields must be initialized manually.