我有兩個類作者和圖書的:類關係,NullPointerException異常錯誤
public class Authors extends RealmObject {
@PrimaryKey
private String url_base;
private RealmList<Books> books;
... getters & setters...
public RealmList<Books> getBooks() {
return books;
}
public void setBooks(RealmList<Books> books) {
this.books = books;
}
}
public class Books extends RealmObject {
@PrimaryKey
private String url_base;
private Authors author;
... getters & setters...
public Authors getAuthor() {
return author;
}
public void setAuthor(Authors author) {
this.author = author;
}
}
執行插入:
Authors author = new Authors();
author.setUrl_base("url_base")
Books book = new Books();
book.setUrl_base("lala");
book.setAuthor(author);
author.getBooks().add(book); // error comes here
realm.beginTransaction();
realm.copyToRealmOrUpdate(author);
realm.commitTransaction();
而且程序提供了顯示java.lang.NullPointerException。
錯誤行:author.getBooks()。Add(book);
什麼可能是錯的?我請求你的幫助。
提供您的getter和setter方法的代碼爲兩個類 – Kushal 2015-04-01 07:17:54
更新問題 – SmallSani 2015-04-01 07:28:00
貌似前你的getBooks返回null,請檢查你在哪裏設置書籍作者。 – Satty 2015-04-01 07:32:40