前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >the bind value at index 2 is null

the bind value at index 2 is null

作者头像
103style
发布2022-12-19 13:17:38
5160
发布2022-12-19 13:17:38
举报
文章被收录于专栏:Android开发经验分享

Greendao 条件查询数据报错 the bind value at index 2 is null

导致报错的方法:

  • xxxDao.queryBuilder().where(xxxDao.Properties.XXX.eq(value).unique()
  • xxxDao.queryBuilder().where(xxxDao.Properties.XXX.notEq(value).unique()

先说下解决方法:就是对以上的方法的调用传入的 value值做空判断。

比如: xxxDao.queryBuilder().where(xxxDao.Properties.XXX.eq(value == null ? "" : value).unique()

或者写一个统一处理的类,类似下面这样,

代码语言:javascript
复制
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.WhereCondition;

public class CheckNullProperty {

    public static WhereCondition eq(Property property, Object value) {
        if (value == null) {
            value = "";
        }
        return property.eq(value);
    }
    ...
}

然后这样调用 xxxDao.queryBuilder().where(CheckNullProperty.eq(xxxDao.Properties.XXX, value))


报错的原因是 org.greenrobot.greendao.Property 类方法传入的 valuenull 导致的。

查看 以下.eq(value)的源代码可以知道,当 valuenull 时,返回的 PropertyCondition 的 value 也是 null

代码语言:javascript
复制
/** Creates an "equal ('=')" condition  for this property. */
public WhereCondition eq(Object value) {
  return new PropertyCondition(this, "=?", value);
}
代码语言:javascript
复制
public PropertyCondition(Property property, String op, Object value) {
    super(checkValueForType(property, value));
    ...
}
代码语言:javascript
复制
private static Object checkValueForType(Property property, Object value) {
    if (value != null && value.getClass().isArray()) {
        throw new DaoException("Illegal value: found array, but simple object required");
    }
    Class<?> type = property.type;
    if (type == Date.class) {
        ...
    } else if (property.type == boolean.class || property.type == Boolean.class) {
        if (value instanceof Boolean) {
            return ((Boolean) value) ? 1 : 0;
        } else if (value instanceof Number) {
           ...
        } else if (value instanceof String) {
           ...
        }
    }
    return value;
}

然后通过 断点 跟踪,发现最后报错的源头 android.database.sqlite.SQLiteProgram

代码语言:javascript
复制
public void bindString(int index, String value) {
    if (value == null) {
        throw new IllegalArgumentException("the bind value at index " + index + " is null");
    }
    bind(index, value);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-04-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Greendao 条件查询数据报错 the bind value at index 2 is null
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档