简体   繁体   English

主键值不能为null。 领域

[英]Primary key value must not be null. Realm

I have weird issue using Realm, I'm new in it. 我在使用Realm时遇到了奇怪的问题,我是新手。 My model has @PrimaryKey private String id; 我的模型具有@PrimaryKey私有字符串ID; field, when I trying to save model into realm my app crashes with this exception. 字段,当我尝试将模型保存到领域中时,此异常使我的应用程序崩溃。

11-25 10:14:55.014 22175-22175/? D/myTag: false
java.lang.IllegalArgumentException: Primary key value must not be null.

Model has name Run, here is how I use it. 模型的名称为Run,这是我的用法。

@Override
public void add(Run run) {
    realm.beginTransaction();

        int lastId;
        RealmResults<Run> all = realm.where(Run.class).findAll();
        if(all.isEmpty())
            run.setId("0");
        else {
            lastId = Integer.parseInt(all.last().getId());
            run.setId(String.valueOf(lastId + 1));
        }
        Log.d("myTag",""+(run.getId()==null));
        realm.copyToRealmOrUpdate(run);

    realm.commitTransaction();
}

My model: 我的模特:

public class Run extends RealmObject {
@PrimaryKey
private String id;
private long activeShiftId;
private long storeId;
private int runState;
private RealmList<Order> orders;
private double totalRevenue;
private double creditRevenue;
private long estimatedTime;
...
}

Log says false. 日志显示错误。 What am I doing wrong? 我究竟做错了什么?

java.lang.IllegalArgumentException: Primary key value must not be null.

Solve this problem, I forget about my collection inside my Run model, set id to every item and it works now. 解决此问题,我忘记了我在Run模型中的集合,将id设置为每个项目,现在可以使用了。

If you have collection of RealmObject in your RealmObject, you should set id(primary key) to every item in that collection and set primary key to external model's field. 如果您的RealmObject中有RealmObject的集合,则应为该集合中的每个项目设置id(主键),并将主键设置为外部模型的字段。

From the given code snippet I can guess that, in your model, Id is a primary key field(which should not be null by default). 从给定的代码片段中,我可以猜到,在您的模型中, Id是主键字段(默认情况下不应为null)。 that is why it is raising the error. 这就是为什么它会引发错误。

this line is setting it to null- 这行将其设置为null-

  run.setId("0");

try to modify this line according to your program logic. 尝试根据您的程序逻辑修改此行。

As of Realm 0.89.0 field, annotated with @PrimaryKey , could be null. Realm 0.89.0开始 ,以@PrimaryKey注释的字段可以为null。 See CHANGELOG . 参见CHANGELOG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EclipseLink-在执行查询期间从该行读取的主键被检测为null。 主键不得包含null - EclipseLink - The primary key read from the row during the execution of the query was detected to be null. Primary keys must not contain null 具有空值的 JPA 复合主键 - JPA composite primary key with null value 外键未获取主键Id的值将显示NULL - Foreign Key not getting the value of Id of Primary Key prints NULL 自动递增主键值给出空值错误 - auto increment primary key value gives null values error 如何防止“提供的文档路径不得为 null”。 火库? - How to prevent “Provided document path must not be null.” Firestore? sqlite约束异常主键必须唯一 - sqlite constraint exception primary key must be unique 通过Android Java中的主键获取Realm对象的正确方法 - Proper way to get Realm object by its primary key in Android Java 您必须使用 @NonNull (复合主键)注释主键 - You must annotate primary keys with @NonNull (composite primary key) Azure 函数,“System.Private.CoreLib:值不能为 null。(参数‘path1’)。值不能为 null。(参数‘provider’)” - Azure Functions, "System.Private.CoreLib: Value cannot be null. (Parameter 'path1'). Value cannot be null. (Parameter 'provider')" JPA 主键值始终为0 - JPA primary key value is always 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM