{"record":{"id":"0a57d9f4cb208586","repo":"HMCL-dev/HMCL","slug":"json-object-cannot-be-null","errorCode":null,"errorMessage":"Json object cannot be null.","messagePattern":"Json object cannot be null\\.","errorType":"validation","errorClass":"JsonParseException","httpStatus":null,"severity":"error","filePath":"HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/PackMcMeta.java","lineNumber":48,"sourceCode":"\nimport java.io.IOException;\nimport java.lang.reflect.Type;\nimport java.nio.file.Files;\nimport java.nio.file.Path;\nimport java.util.ArrayList;\nimport java.util.List;\n\nimport static org.jackhuang.hmcl.util.logging.Logger.LOG;\n\n@JsonSerializable\npublic record PackMcMeta(@SerializedName(\"pack\") PackInfo pack) implements Validation {\n\n    private static final Gson LENIENT_GSON = JsonUtils.defaultGsonBuilder().setStrictness(Strictness.LENIENT).create();\n\n    public static PackMcMeta fromNonNullJson(String jsonString) throws JsonParseException {\n        PackMcMeta parsed = LENIENT_GSON.fromJson(jsonString, PackMcMeta.class);\n        if (parsed == null)\n            throw new JsonParseException(\"Json object cannot be null.\");\n        return parsed;\n    }\n\n    public static PackMcMeta fromNonNullJsonFile(Path jsonFile) throws JsonParseException, IOException {\n        try (var reader = Files.newBufferedReader(jsonFile)) {\n            PackMcMeta parsed = LENIENT_GSON.fromJson(reader, PackMcMeta.class);\n            if (parsed == null)\n                throw new JsonParseException(\"Json object cannot be null.\");\n            return parsed;\n        }\n    }\n\n    @Override\n    public void validate() throws JsonParseException {\n        if (pack == null)\n            throw new JsonParseException(\"pack cannot be null\");\n    }\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/PackMcMeta.java#L30-L66","documentation":"PackMcMeta.fromNonNullJson parses a pack.mcmeta JSON string with a lenient Gson and guarantees a non-null result. If GSON returns null (input is the literal `null`, blank, or empty after lenient parsing), a JsonParseException with this message is thrown instead of silently returning null.","triggerScenarios":"Calling PackMcMeta.fromNonNullJson(\"null\"), fromNonNullJson(\"\"), or with whitespace/blank input.","commonSituations":"Resource packs whose pack.mcmeta is empty; code reading a file that ended up blank; passing unvalidated user-supplied strings.","solutions":["Ensure the input string is a non-empty JSON object like {\"pack\": {...}}","Trim and validate the string is not blank before calling","Re-generate pack.mcmeta for the resource pack"],"exampleFix":"// before\nPackMcMeta.fromNonNullJson(\"\");\n// after\nif (!jsonString.isBlank()) PackMcMeta.fromNonNullJson(jsonString);","handlingStrategy":"validation","validationCode":"if (jsonString == null || jsonString.isBlank() || jsonString.trim().equals(\"null\"))\n    throw new IllegalArgumentException(\"pack.mcmeta content is blank\");","typeGuard":null,"tryCatchPattern":"try {\n    PackMcMeta meta = PackMcMeta.fromNonNullJson(jsonString);\n} catch (JsonParseException e) {\n    log.warn(\"pack.mcmeta parsed to null: \" + e.getMessage());\n}","preventionTips":["Check for blank input before parsing","Read files fully and verify byte length > 0","Catch JsonParseException at the parse boundary"],"tags":["java","json","pack-mcmeta","null"],"backgroundTag":"json-parse-error","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"}