greenrobot/greenDAO · error · DaoException

Illegal boolean value: numbers must be 0 or 1, but was

Error message

Illegal boolean value: numbers must be 0 or 1, but was 

What it means

For boolean-typed properties, checkValueForType() accepts Boolean, numeric 0/1, or the strings "TRUE"/"FALSE" (case-insensitive). A Number whose intValue is neither 0 nor 1 is rejected with this DaoException because SQLite booleans are stored as 0/1 integers.

Solutions

  1. Pass only 0 or 1 for numeric values bound to boolean properties.
  2. Pass a real Boolean (true/false) instead of a number.
  3. If the value is a multi-state flag, change the property type to int rather than boolean.

Example fix

// before
int status = 2;
queryBuilder.where(UserDao.Properties.Active.eq(status)); // DaoException
// after
queryBuilder.where(UserDao.Properties.Active.eq(status == 1));
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = /* candidate */;
if (v instanceof Number) { int i = ((Number) v).intValue();
  if (i != 0 && i != 1) throw new IllegalArgumentException("boolean property needs 0/1"); }

Type guard

boolean isBooleanValue(Object v) {
  if (v instanceof Boolean) return true;
  if (v instanceof Number) { int i = ((Number) v).intValue(); return i == 0 || i == 1; }
  if (v instanceof String) return "TRUE".equalsIgnoreCase((String) v) || "FALSE".equalsIgnoreCase((String) v);
  return false;
}

Try / catch

try { cond = Properties.Active.eq(v); } catch (DaoException e) { cond = Properties.Active.eq(((Number) v).intValue() == 1); }

Prevention

When it happens

Trigger: where(Properties.Active.eq(2)) or any numeric value other than 0/1 bound to a boolean property condition (e.g. a bit mask or a count passed by mistake).

Common situations: Passing flags as ints > 1 (e.g. 2 for 'maybe' or a bitmask) to boolean property conditions, or interop code passing an integer status enum where a boolean is expected.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08). Data as JSON: /api/errors/e2e822cd8dbf9f16. Report an issue: GitHub.

Appendix: source

Thrown at DaoCore/src/main/java/org/greenrobot/greendao/query/WhereCondition.java:92

            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) {
                if (value instanceof Date) {
                    return ((Date) value).getTime();
                } else if (value instanceof Long) {
                    return value;
                } else {
                    throw new DaoException("Illegal date value: expected java.util.Date or Long for value " + value);
                }
            } else if (property.type == boolean.class || property.type == Boolean.class) {
                if (value instanceof Boolean) {
                    return ((Boolean) value) ? 1 : 0;
                } else if (value instanceof Number) {
                    int intValue = ((Number) value).intValue();
                    if (intValue != 0 && intValue != 1) {
                        throw new DaoException("Illegal boolean value: numbers must be 0 or 1, but was " + value);
                    }
                } else if (value instanceof String) {
                    String stringValue = ((String) value);
                    if ("TRUE".equalsIgnoreCase(stringValue)) {
                        return 1;
                    } else if ("FALSE".equalsIgnoreCase(stringValue)) {
                        return 0;
                    } else {
                        throw new DaoException(
                                "Illegal boolean value: Strings must be \"TRUE\" or \"FALSE\" (case insensitive), but was "
                                        + value);
                    }
                }
            }
            return value;
        }

        private static Object[] checkValuesForType(Property property, Object[] values) {

View on GitHub (pinned to 0bbb338e17)