{"record":{"id":"8e9b90de23a2b48c","repo":"greenrobot/greenDAO","slug":"could-not-move-to-cursor-location","errorCode":null,"errorMessage":"Could not move to cursor location ","messagePattern":"Could not move to cursor location ","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/query/LazyList.java","lineNumber":266,"sourceCode":"                    lock.unlock();\n                }\n            }\n            return entity;\n        } else {\n            lock.lock();\n            try {\n                return loadEntity(location);\n            } finally {\n                lock.unlock();\n            }\n        }\n    }\n\n    /** Lock must be locked when entering this method. */\n    protected E loadEntity(int location) {\n        boolean ok = cursor.moveToPosition(location);\n        if(!ok) {\n            throw new DaoException(\"Could not move to cursor location \" + location);\n        }\n        E entity = daoAccess.loadCurrent(cursor, 0, true);\n        if (entity == null) {\n            throw new DaoException(\"Loading of entity failed (null) at position \" + location);\n        }\n        return entity;\n    }\n\n    @Override\n    public int indexOf(Object object) {\n        loadRemaining();\n        return entities.indexOf(object);\n    }\n\n    @Override\n    public boolean isEmpty() {\n        return size == 0;\n    }","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/query/LazyList.java#L248-L284","documentation":"LazyList.loadEntity() positions the underlying SQLite cursor at the requested index before loading the entity. If cursor.moveToPosition(location) returns false — meaning the cursor cannot reach that row — a DaoException is thrown. This happens when the requested index exceeds the rows actually available in the result set.","triggerScenarios":"Calling LazyList.get(index) (directly or via List methods like size()+get) with an index >= the number of cursor rows, typically because the list was mutated/concurrently modified or the index came from a stale size() computation.","commonSituations":"Paging code that trusts a cached size() while the underlying data changed (row deleted by another transaction/thread), or an off-by-one loop like `for (i = 0; i <= list.size(); i++)`.","solutions":["Re-read list.size() immediately before each get() and keep index < size().","Iterate with an Iterator or for-each instead of index-based access on a LazyList.","Use peek(index) if you only want already-loaded entities and tolerate null.","Wrap get() in try-catch for DaoException if the data may change concurrently."],"exampleFix":"// before\nfor (int i = 0; i <= users.size(); i++) { User u = users.get(i); }\n// after\nfor (User u : users) { /* u */ }","handlingStrategy":"try-catch","validationCode":"if (index >= 0 && index < lazyList.size()) { User u = lazyList.get(index); }","typeGuard":null,"tryCatchPattern":"try { u = lazyList.get(i); } catch (DaoException e) { lazyList = query.forCurrentThread().listLazy(); }","preventionTips":["Prefer for-each/Iterator over index access on LazyList.","Never cache size() across database mutations.","Use per-thread queries (forCurrentThread()) with LazyLists."],"tags":["greendao","cursor","index-out-of-range","android"],"backgroundTag":"index-out-of-bounds","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"}