我正在创建一个领域另一个领域对象的领域对象,但是当我运行项目时,我得到一个带有父对象上的领域代理错误的空指针。我检查了两个对象的所有getter和setter,他们正在使用android studio生成的。任何人都可以建议为什么会出现这个错误。
父对象
public class FavouriteObject extends RealmObject {
private String ID;
private String type;
private CategoryObject categoryObject;
public FavouriteObject(){}
public void setID(String ID){
this.ID = ID;
}
public String getID(){
return ID;
}
public void setType(String type) {
this.type = type;
}
public String getType(){
return type;
}
public void setCategoryObject(CategoryObject categoryObject){
this.categoryObject = categoryObject;
}
public CategoryObject getCategoryObject(){
return categoryObject;
}
}
子对象
public class CategoryObject extends RealmObject {
private String dateAdded;
private String description;
private String detailImage;
private String featured;
private String image;
private String langID;
private String navigateUrl;
private String specialFeature;
private String supplierName;
private String weekAdded;
private String title;
private String id;
private boolean newProduct;
public void setNewProduct(boolean newProduct){
this.newProduct = newProduct;
}
public boolean isNewProduct() {
return newProduct;
}
public void setDateAdded(String dateAdded){
this.dateAdded = dateAdded;
}
public String getDateAdded(){
return dateAdded;
}
public void setDescription(String description){
this.description = description;
}
public String getDescription(){
return description;
}
public void setDetailImage(String detailImage){
this.detailImage = detailImage;
}
public String getDetailImage(){
return detailImage;
}
public void setFeatured(String featured){
this.featured = featured;
}
public String getFeatured(){
return featured;
}
public void setImage(String image){
this.image = image;
}
public String getImage(){
return image;
}
public void setLangID(String langID) {
this.langID = langID;
}
public String getLangID(){
return langID;
}
public void setNavigateUrl(String navigateUrl){
this.navigateUrl = navigateUrl;
}
public String getNavigateUrl(){
return navigateUrl;
}
public void setSpecialFeature(String specialFeature) {
this.specialFeature = specialFeature;
}
public String getSpecialFeature(){
return specialFeature;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public String getSupplierName(){
return supplierName;
}
public void setWeekAdded(String weekAdded) {
this.weekAdded = weekAdded;
}
public String getWeekAdded(){
return weekAdded;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle(){
return title;
}
public void setId(String id) {
this.id = id;
}
public String getId(){
return id;
}
}
添加对象
realm.beginTransaction();
FavouritesObject favourite = realm.createObject(FavouritesObject.class);
favourite.setID(po.getID());
favourite.setType("microsite");
favourite.setCategoryObject(productsObject.get(position));
错误讯息
java.lang.NullPointerException
at io.realm.FavouritesObjectRealmProxy.setCategoryObject(FavouritesObjectRealmProxy.java:64)
at com.test.project.Adapters.CategoryAdapter$1.onClick(CategoryAdapter.java:101)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我还尝试创建一个只有一个变量的测试对象,但是我仍然有同样的问题。任何帮助都会很棒!
答案 0 :(得分:2)
行favourite.setCategoryObject(productsObject.get(position));
似乎是个问题。
您从productsObject.get(position)
获得的价值是来自Realm(即它是由RealmResult<ProductsObject>
还是realm.createObject(ProductsObject.class)
或类似产品创建的。
如果您的ProductsObject从RealmObject继承的io.realm.internal.Row row
和io.realm.Realm realm
属性不为null,您就会知道该对象是由Realm创建的。
我建议更改此部分:
favourite.setType("microsite");
favourite.setCategoryObject(productsObject.get(position));
到
favourite.setType("microsite");
ProductsObject po = productsObject.get(position);
favourite.setCategoryObject(po);
然后在ProductsObject po = productsObject.get(position);
行设置一个断点,以查看row
上的realm
或RealmObject
属性(继承自po
) object为null。如果它们中的任何一个或两个都为null,那么您需要更新该函数以确保您的productsObject.get(position)
函数返回由Realm创建的值。