{"record":{"id":"e2e822cd8dbf9f16","repo":"greenrobot/greenDAO","slug":"illegal-boolean-value-numbers-must-be-0-or-1-but","errorCode":null,"errorMessage":"Illegal boolean value: numbers must be 0 or 1, but was ","messagePattern":"Illegal boolean value: numbers must be 0 or 1, but was ","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/query/WhereCondition.java","lineNumber":92,"sourceCode":"            if (value != null && value.getClass().isArray()) {\n                throw new DaoException(\"Illegal value: found array, but simple object required\");\n            }\n            Class<?> type = property.type;\n            if (type == Date.class) {\n                if (value instanceof Date) {\n                    return ((Date) value).getTime();\n                } else if (value instanceof Long) {\n                    return value;\n                } else {\n                    throw new DaoException(\"Illegal date value: expected java.util.Date or Long for value \" + value);\n                }\n            } else if (property.type == boolean.class || property.type == Boolean.class) {\n                if (value instanceof Boolean) {\n                    return ((Boolean) value) ? 1 : 0;\n                } else if (value instanceof Number) {\n                    int intValue = ((Number) value).intValue();\n                    if (intValue != 0 && intValue != 1) {\n                        throw new DaoException(\"Illegal boolean value: numbers must be 0 or 1, but was \" + value);\n                    }\n                } else if (value instanceof String) {\n                    String stringValue = ((String) value);\n                    if (\"TRUE\".equalsIgnoreCase(stringValue)) {\n                        return 1;\n                    } else if (\"FALSE\".equalsIgnoreCase(stringValue)) {\n                        return 0;\n                    } else {\n                        throw new DaoException(\n                                \"Illegal boolean value: Strings must be \\\"TRUE\\\" or \\\"FALSE\\\" (case insensitive), but was \"\n                                        + value);\n                    }\n                }\n            }\n            return value;\n        }\n\n        private static Object[] checkValuesForType(Property property, Object[] values) {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/query/WhereCondition.java#L74-L110","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass only 0 or 1 for numeric values bound to boolean properties.","Pass a real Boolean (true/false) instead of a number.","If the value is a multi-state flag, change the property type to int rather than boolean."],"exampleFix":"// before\nint status = 2;\nqueryBuilder.where(UserDao.Properties.Active.eq(status)); // DaoException\n// after\nqueryBuilder.where(UserDao.Properties.Active.eq(status == 1));","handlingStrategy":"type-guard","validationCode":"Object v = /* candidate */;\nif (v instanceof Number) { int i = ((Number) v).intValue();\n  if (i != 0 && i != 1) throw new IllegalArgumentException(\"boolean property needs 0/1\"); }","typeGuard":"boolean isBooleanValue(Object v) {\n  if (v instanceof Boolean) return true;\n  if (v instanceof Number) { int i = ((Number) v).intValue(); return i == 0 || i == 1; }\n  if (v instanceof String) return \"TRUE\".equalsIgnoreCase((String) v) || \"FALSE\".equalsIgnoreCase((String) v);\n  return false;\n}","tryCatchPattern":"try { cond = Properties.Active.eq(v); } catch (DaoException e) { cond = Properties.Active.eq(((Number) v).intValue() == 1); }","preventionTips":["Bind real Booleans to boolean properties instead of ints.","Model multi-state flags as int properties, not boolean.","Clamp/normalize numeric flags to 0/1 before querying."],"tags":["greendao","where-condition","boolean","android"],"backgroundTag":"invalid-enum-value","analyzedSha":"0bbb338e17c4cf28aba5aae62d42f73f50186157","analyzedAt":"2026-09-08T03:22:36.050Z","contentChangedAt":"2026-09-08T03:22:36.050Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}