{"record":{"id":"1f6b7616ccf19c78","repo":"HMCL-dev/HMCL","slug":"code-is-null","errorCode":null,"errorMessage":"code is null","messagePattern":"code is null","errorType":"validation","errorClass":"JsonParseException","httpStatus":null,"severity":"error","filePath":"HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaState.java","lineNumber":191,"sourceCode":"        private final String code;\n\n        @SerializedName(\"profile_index\")\n        private final int profileIndex;\n\n        @SerializedName(\"profiles\")\n        private final List<TerracottaProfile> profiles;\n\n        HostOK(int port, int index, String state, String code, int profileIndex, List<TerracottaProfile> profiles) {\n            super(port, index, state);\n            this.code = code;\n            this.profileIndex = profileIndex;\n            this.profiles = profiles;\n        }\n\n        @Override\n        public void validate() throws JsonParseException, TolerableValidationException {\n            if (code == null) {\n                throw new JsonParseException(\"code is null\");\n            }\n            if (profiles == null) {\n                throw new JsonParseException(\"profiles is null\");\n            }\n        }\n\n        public String getCode() {\n            return code;\n        }\n\n        public List<TerracottaProfile> getProfiles() {\n            return profiles;\n        }\n\n        @Override\n        public boolean isForkOf(TerracottaState state) {\n            return state instanceof HostOK hostOK && this.index - hostOK.index <= profileIndex;\n        }","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaState.java#L173-L209","documentation":"TerracottaState.State.validate enforces that the required 'code' member is present after JSON deserialization; a missing code means the state object is incomplete and cannot be used, so JsonParseException is thrown.","triggerScenarios":"Deserializing a terracotta state JSON that omits the code field — truncated file, older schema, or hand-edited config.","commonSituations":"Hand-editing the terracotta state file; version/schema drift dropping the code field; partial writes.","solutions":["Add the required 'code' field to the state JSON","Regenerate the terracotta state through the normal flow instead of editing the file manually","Restore the file from backup if it was truncated","Catch JsonParseException at load and re-initialize the terracotta state"],"exampleFix":"// before\n{\"profiles\":[...]}\n// after\n{\"code\":\"AUTH-CODE\",\"profiles\":[...]}","handlingStrategy":"type-guard","validationCode":"if (parsedState.get(\"code\") == null || parsedState.get(\"code\").isJsonNull()) { throw new IllegalArgumentException(\"state.code required\"); }","typeGuard":"static boolean hasNonNull(JsonObject o, String key) { return o.has(key) && !o.get(key).isJsonNull(); }","tryCatchPattern":"try { state.validate(); } catch (JsonParseException e) { /* re-run terracotta setup to regenerate state */ }","preventionTips":["Never omit required fields when serializing state","Use the library's write path for state files","Validate after every manual or scripted edit"],"tags":["json","validation","config"],"backgroundTag":"missing-required-config-field","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}