获取NullPointer将对象添加到RealmList

时间:2015-06-23 14:09:52

标签: android realm

当我尝试在Realm数据库Android中向RealmList添加新对象时出现问题。我总是得到NullPointer错误:

  

Global.USER_LOGGED.getProductsGrid()添加(pFinal);

这是我的模特:

    @RealmClass
    public class UserRealm extends RealmObject implements Serializable {

        private String    email;
        private String    password;
        private String    tokenId;
        private String    tokenSecret;
        private String    creationDate;
        private RealmList<ProductRealm> productsGrid;   //Temp table to load category products
        private CartRealm cart;        //One-to-one relationship

    /*GETTERS & SETTERS*/
    ... 
}


    @RealmClass
    public class ProductRealm extends RealmObject implements Serializable {

        private String  catId;   //Only one category per product. In generic case many category per product
        private String  entityId;
        private String  parentId;
        private String  typeId;
        private String  sku;
        private String  canaryProduct;
        private String  premium;
        private String  brand;
        private String  dinitosQty;
        private String  name;
        private String  ean;
        private String  description;
        private String  shortDescription;
        private String  attributeSet;
        private String  weight;
        private String  isSaleable;
        private String  imageUrl;
        private String  unit;
        private String  unitFresh;
        private String  childrenSelected;
        private float   price;
        private float   minWeight;
        private float   quantityWeight;
        private int     quantity;
        private int     stock;
        private int     typeOfAttribute;    //0: Per units | 1: Per weight
        private RealmList<AttributeRealm> attributeItems;
        private RealmList<ProductRealm>   children;  

    /* GETTERS & SETTERS */
    ... 
}

这是代码:

public static void parseProducts(String result){
        Logs.MessageLogs("parseProducts", result, "v", Global.SHOW_LOGS);
        Object product = null;
        JSONObject json = null;
        ProductRealm pFinal, p = new ProductRealm();

        try {
            json = new JSONObject(result);

            if(Global.USER_LOGGED.getProductsGrid().size() > 0)
                _CRUDDatabase.clearProductsGrid(Global.REALM);

            for (Iterator<?> iterator = json.keys(); iterator.hasNext(); ) {
                String key = (String) iterator.next();
                if (json.get(key) instanceof JSONObject) {    //Category with children
                    product = ((JSONObject) json.get(key));
                    Global.REALM.beginTransaction();
                    pFinal = Global.REALM.createObject(ProductRealm.class);

                    pFinal = _CRUDDatabase.createNewProduct(Global.PREVIOUS_CATEGORY.getId(), product);
                    Global.USER_LOGGED.getProductsGrid().add(pFinal); 
                    Global.REALM.commitTransaction();
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }

Global.USSER_LOGGED是一个Singleton,当前用户已登录:

  

public static UserRealm USER_LOGGED;

堆栈追踪:

06-23 15:16:32.619 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? java.lang.NullPointerException 
06-23 15:16:32.619 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at io.realm.RealmList.add(RealmList.java:122) 
06-23 15:16:32.619 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at com.singularfactory.ecommerceapp.utility.UtilityDB.parseProducts(UtilityDB.java:283)
06-23 15:16:32.619 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at com.singularfactory.ecommerceapp.fragment.ShopViewPagerFragment.OAuthResponse(ShopViewPagerFragment.java:148)
06-23 15:16:32.623 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at com.singularfactory.ecommerceapp.utility.WebService$getProductsByCategoryIdWithOAuth10a.onPostExecute(WebService.java:239)
06-23 15:16:32.623 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at com.singularfactory.ecommerceapp.utility.WebService$getProductsByCategoryIdWithOAuth10a.onPostExecute(WebService.java:187)
06-23 15:16:32.623 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at android.os.AsyncTask.finish(AsyncTask.java:632) 
06-23 15:16:32.639 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-23 15:16:32.639 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
06-23 15:16:32.639 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at android.os.Looper.loop(Looper.java:136) 
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at android.app.ActivityThread.main(ActivityThread.java:5001) 
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at java.lang.reflect.Method.invokeNative(Native Method)
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at java.lang.reflect.Method.invoke(Method.java:515)
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
06-23 15:16:32.643 17555-17555/com.singularfactory.ecommerceapp.ecommerceappW/System.err? at dalvik.system.NativeStart.main(Native Method)

我正在阅读Realm doc,我发现创建RealmList有两种模式:托管和非托管。我正在尝试创建两者,但我总是有同样的错误。

如何在数据库中创建对象并分配给RealmList? 我的代码有什么问题?

提前致谢:)

更新

我的单身人士课程:

public class Global {
    private static Global instance = null;
...
//Realm
    public static Realm REALM;              //Database Realm
    public static String DATABASE_NAME;     //Name of Realm database
    public static UserRealm USER_LOGGED;            //Object of the user logged

...
private Global() {
//Realm
        REALM   = null;
        DATABASE_NAME = "keymarket.realm";
        USER_LOGGED = null;
...
}

public static Global getInstance(){
        if(instance == null){
            instance = new Global();
        }
        return instance;
    }

1 个答案:

答案 0 :(得分:2)

在将对象添加到列表之前,需要将对象添加到域中,因为内部Realm将调用getId(或任何获取它的主键的方法)以将对象添加到列表中。由于它使用代理方法并且未将对象添加到领域,因此将获得空指针异常..