领域:如何将int设置为可空,以便如果从json返回值“null”,则可以忽略它

时间:2017-08-29 14:47:09

标签: android json realm nullable primitive

我有以下对象:

    layout = MytypeField()

但是每当我得到一个json响应时,int(或原语)为null,我在Realm中得到一个错误:

08-29 16:45:59.220:I / WSCalls(6370):错误:尝试将不可为空的字段'departure_time'设置为null。

我从Realm 3.5中读到了它可以设置空值,或设置默认版本,但我还没有管理。 怎么可能?我看到@Nullable没有帮助

1 个答案:

答案 0 :(得分:3)

首先:

int是原始数据类型,不能与null一起使用。

<强>解决方案:

您可以将类型更改为IntegerInteger可以包含null,但由于装箱/拆箱而增加了一点开销。 (见:https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html