{"record":{"id":"9c39c6ea5522be73","repo":"greenrobot/greenDAO","slug":"no-entity-found-for-query","errorCode":null,"errorMessage":"No entity found for query","messagePattern":"No entity found for query","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/query/Query.java","lineNumber":142,"sourceCode":"     * @return Entity or null if no matching entity was found\n     * @throws DaoException if the result is not unique\n     */\n    public T unique() {\n        checkThread();\n        Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);\n        return daoAccess.loadUniqueAndCloseCursor(cursor);\n    }\n\n    /**\n     * Executes the query and returns the unique result (never null).\n     *\n     * @return Entity\n     * @throws DaoException if the result is not unique or no entity was found\n     */\n    public T uniqueOrThrow() {\n        T entity = unique();\n        if (entity == null) {\n            throw new DaoException(\"No entity found for query\");\n        }\n        return entity;\n    }\n\n    @Override\n    public Query<T> setParameter(int index, Object parameter) {\n        return (Query<T>) super.setParameter(index, parameter);\n    }\n\n    @Override\n    public Query<T> setParameter(int index, Date parameter) {\n        return (Query<T>) super.setParameter(index, parameter);\n    }\n\n    @Override\n    public Query<T> setParameter(int index, Boolean parameter) {\n        return (Query<T>) super.setParameter(index, parameter);\n    }","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/query/Query.java#L124-L160","documentation":"Query.uniqueOrThrow() calls unique() and throws a DaoException if no row matched the query, distinguishing 'no result' (exception) from the safe unique() which just returns null. The library throws this so callers that require an entity can get a fail-fast error instead of a NullPointerException later.","triggerScenarios":"Calling uniqueOrThrow() on a Query whose WHERE condition matched zero rows — e.g. queryBuilder.where(Properties.Id.eq(missingId)).uniqueOrThrow().","commonSituations":"Lookups by primary key or unique column where the key came from user input, an external API, or a stale reference to a deleted row.","solutions":["Use unique() instead of uniqueOrThrow() and handle the null return.","Check existence first (e.g. queryBuilder...count() or a select on the key) before uniqueOrThrow().","Wrap in try-catch for DaoException when absence is an expected business case."],"exampleFix":"// before\nUser u = queryBuilder.where(UserDao.Properties.Email.eq(email)).uniqueOrThrow();\n// after\nUser u = queryBuilder.where(UserDao.Properties.Email.eq(email)).unique();\nif (u == null) { /* handle missing user */ }","handlingStrategy":"try-catch","validationCode":"long count = queryBuilder.where(UserDao.Properties.Email.eq(email)).count();\nboolean exists = count > 0;","typeGuard":null,"tryCatchPattern":"try { u = q.uniqueOrThrow(); } catch (DaoException e) { /* create default or report not-found */ }","preventionTips":["Default to unique() and handle null unless absence is truly exceptional.","Validate lookup keys before querying.","Check row existence with count() for hot paths."],"tags":["greendao","query","android","database"],"backgroundTag":"record-not-found","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"}