Andy Joyce
Andy Joyce

Reputation: 2832

RealmProxy error on Realm Object

Im currently creating a realm object that takes another realm object however when i run the project i get a null pointer with the realm proxy error on the parent object. Ive checked all of the getters and setters for both objects and they are using the android studio generated ones. Can anyone suggest why this error may be appearing.

Parent object

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

}

Child Object

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

Adding the Object

realm.beginTransaction();
FavouritesObject favourite = realm.createObject(FavouritesObject.class);
favourite.setID(po.getID());
favourite.setType("microsite");
favourite.setCategoryObject(productsObject.get(position));

The Error Message

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)

I have also tried to create a test object that only had one variable however i still had the same issue. Any help would be great!

Upvotes: 1

Views: 3829

Answers (1)

Owen Lilly
Owen Lilly

Reputation: 171

The line favourite.setCategoryObject(productsObject.get(position)); seems to be the issue.

The value you get from productsObject.get(position), is it coming from Realm (i.e. has it been created as a result of RealmResult<ProductsObject> or realm.createObject(ProductsObject.class) or similar).

You'll know that the object is being created by Realm if the io.realm.internal.Row row and the io.realm.Realm realm properties that your ProductsObject inherits from the RealmObject isn't null.

I suggest changing this section:

favourite.setType("microsite");
favourite.setCategoryObject(productsObject.get(position));

to

favourite.setType("microsite");
ProductsObject po = productsObject.get(position);
favourite.setCategoryObject(po);

then set a break point on the line ProductsObject po = productsObject.get(position); to see if the either the row or realm property (inherited from RealmObject) on the po object is null. If any or both of them is null then you need to update the function to ensure that your productsObject.get(position) function returns a value created by Realm.

Upvotes: 2

Related Questions