{"record":{"id":"31b70174f1a47abc","repo":"NationalSecurityAgency/ghidra","slug":"could-not-fetch-key-value-key","errorCode":null,"errorMessage":"Could not fetch key value: {key}","messagePattern":"Could not fetch key value: (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/client/tables/KeyValueTable.java","lineNumber":159,"sourceCode":"\t}\n\n\t/**\n\t * \n\t * @param key the key to get the value for\n\t * @return the value associated with key or throw exception if key not present\n\t * @throws SQLException if the sql statement cannot be parsed\n\t */\n\tpublic String getValue(String key) throws SQLException {\n\t\tPreparedStatement s =\n\t\t\tselectStatement.prepareIfNeeded(() -> db.prepareStatement(SELECT_STMT));\n\t\ts.setString(1, key);\n\t\ttry (ResultSet rs = s.executeQuery()) {\n\t\t\tString value;\n\t\t\tif (rs.next()) {\n\t\t\t\tvalue = rs.getString(1);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new SQLException(\"Could not fetch key value: \" + key);\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t}\n\n}\n","sourceCodeStart":141,"sourceCodeEnd":166,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/client/tables/KeyValueTable.java#L141-L166","documentation":"Thrown by KeyValueTable.getValue when a SELECT for the given key returns no rows. The keyvalue table stores named configuration/metadata entries (e.g., version, layout flags); requesting a key that is absent is treated as an error. This usually means the database was not fully initialized or the key name is wrong.","triggerScenarios":"Calling getValue(key) with a key that has no row in the keyvalue table. Occurs during database open if expected metadata keys (version, layout) are missing — i.e., the database was created by an older/newer BSim version that uses a different key set, or initialization was interrupted before writing all keys.","commonSituations":"Opening a BSim database created by a different BSim version whose keyvalue schema differs. Partially initialized database missing required keys. Typo or changed key name between versions. Database that was copied without its keyvalue rows.","solutions":["Check that the key name matches the current BSim version's expected keyvalue entries.","If the key is optional, catch the exception and supply a default rather than failing.","Re-initialize or upgrade the database so all required keyvalue rows are present.","Inspect the keyvalue table directly to see which keys exist and compare against expected."],"exampleFix":"// before\nString version = kvTable.getValue(\"version\"); // throws if absent\n\n// after\nString version;\ntry {\n    version = kvTable.getValue(\"version\");\n} catch (SQLException e) {\n    version = defaultVersion; // fallback for older/uninitialized databases\n    log.warning(\"version key missing; using default \" + version);\n}","handlingStrategy":"fallback","validationCode":"// Check the key exists before fetching, if a contains helper is available;\n// otherwise wrap in try-catch and supply a default.\nString key = \"version\";","typeGuard":null,"tryCatchPattern":"String value;\ntry {\n    value = kvTable.getValue(key);\n} catch (SQLException e) {\n    if (e.getMessage().contains(\"Could not fetch key value\")) {\n        value = defaults.getOrDefault(key, null);\n        log.warning(\"Missing keyvalue '\" + key + \"'; using default\");\n    } else throw e;\n}","preventionTips":["Initialize all required keyvalue rows when creating a BSim database.","Match key names to the BSim client version in use.","For optional keys, catch and supply documented defaults."],"tags":["bsim","database","key-value","missing-key","version-mismatch","sql-exception"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}