{"record":{"id":"9b1c128c2b828e63","repo":"HMCL-dev/HMCL","slug":"json-object-cannot-be-null-9b1c12","errorCode":null,"errorMessage":"Json object cannot be null.","messagePattern":"Json object cannot be null\\.","errorType":"exception","errorClass":"JsonParseException","httpStatus":null,"severity":"error","filePath":"HMCLCore/src/main/java/org/jackhuang/hmcl/util/gson/JsonUtils.java","lineNumber":464,"sourceCode":"    /// @throws JsonParseException if the JSON is `null` or cannot be deserialized\n    public static <T> T fromNonNullJson(String json, Class<T> classOfT) throws JsonParseException {\n        return fromNonNullJson(GSON, json, classOfT);\n    }\n\n    /// Deserializes a JSON string into a non-null object described by the given [TypeToken]\n    /// using the provided [Gson] instance.  Throws [JsonParseException] if the result would be\n    /// `null`.\n    ///\n    /// @param <T>  the target type\n    /// @param gson the [Gson] instance to use\n    /// @param json the JSON string to parse\n    /// @param type a [TypeToken] describing the target type\n    /// @return the deserialized object, never `null`\n    /// @throws JsonParseException if the JSON is `null` or cannot be deserialized\n    public static <T> T fromNonNullJson(Gson gson, String json, TypeToken<T> type) throws JsonParseException {\n        T parsed = fromJson(gson, json, type);\n        if (parsed == null)\n            throw new JsonParseException(\"Json object cannot be null.\");\n        return parsed;\n    }\n\n    /// Deserializes a JSON string into a non-null object described by the given [TypeToken]\n    /// using [GSON].  Throws [JsonParseException] if the result would be `null`.\n    ///\n    /// @param <T>  the target type\n    /// @param json the JSON string to parse\n    /// @param type a [TypeToken] describing the target type\n    /// @return the deserialized object, never `null`\n    /// @throws JsonParseException if the JSON is `null` or cannot be deserialized\n    public static <T> T fromNonNullJson(String json, TypeToken<T> type) throws JsonParseException {\n        return fromNonNullJson(GSON, json, type);\n    }\n\n    /// Reads and deserializes JSON from an [InputStream] (UTF-8) into a non-null object of the\n    /// given class using the provided [Gson] instance.  The stream is closed after reading.\n    /// Throws [JsonParseException] if the result would be `null`.","sourceCodeStart":446,"sourceCodeEnd":482,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCLCore/src/main/java/org/jackhuang/hmcl/util/gson/JsonUtils.java#L446-L482","documentation":"JsonUtils.fromNonNullJson deserializes a JSON string and requires a non-null result. This JsonParseException is thrown when the JSON parses to null (e.g. the input string is \"null\", empty content, or the target maps to a null value).","triggerScenarios":"Calling fromNonNullJson(gson, json, type) where json is \"null\", an empty file/string, or a JSON document whose deserialization yields null.","commonSituations":"Reading an empty or reset config file, a server returning the literal body \"null\", or code reading a deleted/never-initialized persisted object.","solutions":["Check the input string is non-empty, valid JSON before calling fromNonNullJson (e.g. !json.isBlank()).","Fix the data source so it emits a real JSON object instead of \"null\".","If null is legitimately possible, use JsonUtils.fromJson and handle the null result instead.","Initialize the config/store with a default object on first run."],"exampleFix":"// before\nT obj = JsonUtils.fromNonNullJson(gson, maybeEmpty, type); // throws on \"null\"\n// after\nT obj = maybeEmpty == null || maybeEmpty.isBlank() ? defaultObj\n        : JsonUtils.fromNonNullJson(gson, maybeEmpty, type);","handlingStrategy":"validation","validationCode":"if (json == null || json.isBlank() || \"null\".equals(json.trim()))\n    return defaultObject; // avoid fromNonNullJson","typeGuard":"static boolean isNonEmptyJson(String json) {\n    return json != null && !json.isBlank() && !\"null\".equals(json.trim());\n}","tryCatchPattern":"try {\n    return JsonUtils.fromNonNullJson(gson, json, typeToken);\n} catch (JsonParseException e) {\n    log.warn(\"Null/empty JSON, using defaults\", e);\n    return defaultObject;\n}","preventionTips":["Check config files are non-empty before loading.","Treat literal \"null\" bodies from servers as missing data.","Write default objects on first run so stores are never empty.","Use fromJson with a null check when null results are legitimate."],"tags":["gson","deserialization","null"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}