{"record":{"id":"059ff803438e2bd6","repo":"TheAlgorithms/Java","slug":"table-is-empty-cannot-find-keys","errorCode":null,"errorMessage":"Table is empty; cannot find keys.","messagePattern":"Table is empty; cannot find keys\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java","lineNumber":193,"sourceCode":"                System.out.println(\"Bucket \" + i + \": \" + buckets[i].toString());\n            }\n        }\n        System.out.println();\n    }\n\n    /**\n     * Finds the index of a given key in the hash table.\n     *\n     * @param key the key to be found\n     * @return the index where the key is located\n     * @throws IllegalArgumentException if the table is empty or the key is not found\n     */\n    public int findKeyInTable(int key) {\n        Integer wrappedInt = key;\n        int hash = hashFunction1(key);\n\n        if (isEmpty()) {\n            throw new IllegalArgumentException(\"Table is empty; cannot find keys.\");\n        }\n\n        if (Objects.equals(buckets[hash], wrappedInt)) {\n            return hash;\n        }\n\n        hash = hashFunction2(key);\n        if (!Objects.equals(buckets[hash], wrappedInt)) {\n            throw new IllegalArgumentException(\"Key \" + key + \" not found in the table.\");\n        } else {\n            return hash;\n        }\n    }\n\n    /**\n     * Checks if the given key is present in the hash table.\n     *\n     * @param key the key to be checked","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java#L175-L211","documentation":"Thrown by HashMapCuckooHashing.findKeyInTable(int) when the table holds no keys. Cuckoo hashing places each key in one of exactly two bucket positions computed by hashFunction1/hashFunction2, so a lookup is only meaningful when at least one bucket is occupied. isEmpty() scans every bucket and returns true only when all are null, so the guard fires on a never-seeded table or one fully drained by deletions.","triggerScenarios":"Calling findKeyInTable(key) immediately after `new HashMapCuckooHashing(7)` with no prior insertKey2HashTable calls; calling it after a loop of deleteKeyFromHashTable has removed every key so size reaches 0 and all buckets are null again.","commonSituations":"Application startup paths that query before seeding data; test scaffolding that asserts on an unpopulated map; bulk-delete routines that then attempt a lookup without re-checking occupancy.","solutions":["Guard the call with `if (!h.isEmpty()) h.findKeyInTable(key);` so an empty table skips the lookup.","Ensure at least one key is inserted via insertKey2HashTable before any findKeyInTable call in the same flow.","Wrap the call in try/catch(IllegalArgumentException) and treat the exception as 'key absent'."],"exampleFix":"// before\nint idx = h.findKeyInTable(key);\n\n// after\nint idx = h.isEmpty() ? -1 : (h.checkTableContainsKey(key) ? h.findKeyInTable(key) : -1);","handlingStrategy":"validation","validationCode":"// Run before findKeyInTable\nif (h.isEmpty()) {\n    // no keys present; do not call findKeyInTable\n    return -1; // or handle 'empty' case\n}\nint idx = h.findKeyInTable(key);","typeGuard":null,"tryCatchPattern":"try {\n    int idx = h.findKeyInTable(key);\n} catch (IllegalArgumentException e) {\n    // table empty OR key absent; treat as not-found\n}","preventionTips":["Always seed the table with at least one key before exposing lookup operations.","Keep a flag or counter tracking whether any insert has succeeded.","In test code, assert non-empty before asserting on findKeyInTable results."],"tags":["hashmap","cuckoo-hashing","precondition","validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}