the bind value at index 2 is null

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()

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

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

/** Creates an "equal ('=')" condition  for this property. */
public WhereCondition eq(Object value) {
  return new PropertyCondition(this, "=?", value);
}
public PropertyCondition(Property property, String op, Object value) {
    super(checkValueForType(property, value));
    ...
}
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

public void bindString(int index, String value) {
    if (value == null) {
        throw new IllegalArgumentException("the bind value at index " + index + " is null");
    }
    bind(index, value);
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值