gpt4 book ai didi

android - 获取 NullPointer 将对象添加到 RealmList

转载 作者:行者123 更新时间:2023-11-29 20:40:51 24 4
gpt4 key购买 nike

当我尝试向 Android Realm 数据库中的 RealmList 添加新对象时遇到问题。我总是在以下位置收到 NullPointer 错误:

Global.USER_LOGGED.getProductsGrid().add(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 是一个单例,当前用户登录:

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 文档,发现有两种创建 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;
}

最佳答案

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

关于android - 获取 NullPointer 将对象添加到 RealmList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com