{"record":{"id":"e0ab72e67e566af6","repo":"greenrobot/greenDAO","slug":"limit-must-be-set-with-querybuilder-before-it-can","errorCode":null,"errorMessage":"Limit must be set with QueryBuilder before it can be used here","messagePattern":"Limit must be set with QueryBuilder before it can be used here","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/query/AbstractQueryWithLimit.java","lineNumber":57,"sourceCode":"     * Sets the parameter (0 based) using the position in which it was added during building the query. Note: all\n     * standard WHERE parameters come first. After that come the WHERE parameters of joins (if any).\n     */\n    public AbstractQueryWithLimit<T> setParameter(int index, Object parameter) {\n        if (index >= 0 && (index == limitPosition || index == offsetPosition)) {\n            throw new IllegalArgumentException(\"Illegal parameter index: \" + index);\n        }\n        return (AbstractQueryWithLimit<T>) super.setParameter(index, parameter);\n    }\n\n    /**\n     * Sets the limit of the maximum number of results returned by this Query. {@link\n     * org.greenrobot.greendao.query.QueryBuilder#limit(int)} must\n     * have been called on the QueryBuilder that created this Query object.\n     */\n    public void setLimit(int limit) {\n        checkThread();\n        if (limitPosition == -1) {\n            throw new IllegalStateException(\"Limit must be set with QueryBuilder before it can be used here\");\n        }\n        parameters[limitPosition] = Integer.toString(limit);\n    }\n\n    /**\n     * Sets the offset for results returned by this Query. {@link org.greenrobot.greendao.query.QueryBuilder#offset(int)} must\n     * have been called on\n     * the QueryBuilder that created this Query object.\n     */\n    public void setOffset(int offset) {\n        checkThread();\n        if (offsetPosition == -1) {\n            throw new IllegalStateException(\"Offset must be set with QueryBuilder before it can be used here\");\n        }\n        parameters[offsetPosition] = Integer.toString(offset);\n    }\n\n}","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/query/AbstractQueryWithLimit.java#L39-L75","documentation":"setLimit only rewrites an existing LIMIT placeholder parameter. If the Query was built without QueryBuilder.limit(int), limitPosition is -1 and there is no slot to write, so IllegalStateException is thrown.","triggerScenarios":"Calling query.setLimit(n) on a Query created by queryBuilder()...build() where .limit() was never invoked during building.","commonSituations":"Attempting to adjust paging dynamically after building a query; copying query-building code that omitted .limit(); changing requirements from unpaginated to paginated queries.","solutions":["Add .limit(n) in the QueryBuilder before build() so the LIMIT parameter slot exists","Rebuild the Query with QueryBuilder when the limit requirement changes","Keep a cached QueryBuilder rather than only the built Query if limits vary at runtime"],"exampleFix":"// before\nQuery<User> q = userDao.queryBuilder().where(...).build();\nq.setLimit(10); // throws\n// after\nQuery<User> q = userDao.queryBuilder().where(...).limit(10).build();\nq.setLimit(10); // OK: adjusts existing limit","handlingStrategy":"try-catch","validationCode":"// ensure the query was built with .limit() — track at build time\nQuery<User> q = qb.where(...).limit(initialLimit).build();","typeGuard":null,"tryCatchPattern":"try {\n    query.setLimit(newLimit);\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"Limit must be set with QueryBuilder\")) {\n        query = qb.where(...).limit(newLimit).build();\n    }\n}","preventionTips":["Always add .limit() in the builder if you plan to change limits later","Rebuild queries when pagination requirements change","Cache the QueryBuilder, not only the built Query"],"tags":["greendao","orm","query","pagination"],"backgroundTag":"invalid-state-transition","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"}