HMCL-dev/HMCL · error · JsonParseException

Json object cannot be null.

Error message

Json object cannot be null.

What it means

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.

Solutions

  1. Ensure the input string is a non-empty JSON object like {"pack": {...}}
  2. Trim and validate the string is not blank before calling
  3. Re-generate pack.mcmeta for the resource pack

Example fix

// before
PackMcMeta.fromNonNullJson("");
// after
if (!jsonString.isBlank()) PackMcMeta.fromNonNullJson(jsonString);
Defensive patterns

Strategy: validation

Validate before calling

if (jsonString == null || jsonString.isBlank() || jsonString.trim().equals("null"))
    throw new IllegalArgumentException("pack.mcmeta content is blank");

Try / catch

try {
    PackMcMeta meta = PackMcMeta.fromNonNullJson(jsonString);
} catch (JsonParseException e) {
    log.warn("pack.mcmeta parsed to null: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling PackMcMeta.fromNonNullJson("null"), fromNonNullJson(""), or with whitespace/blank input.

Common situations: Resource packs whose pack.mcmeta is empty; code reading a file that ended up blank; passing unvalidated user-supplied strings.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of HMCL-dev/HMCL@24702dc5a0 (2026-09-10). Data as JSON: /api/errors/0a57d9f4cb208586. Report an issue: GitHub.

Appendix: source

Thrown at HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/PackMcMeta.java:48

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.jackhuang.hmcl.util.logging.Logger.LOG;

@JsonSerializable
public record PackMcMeta(@SerializedName("pack") PackInfo pack) implements Validation {

    private static final Gson LENIENT_GSON = JsonUtils.defaultGsonBuilder().setStrictness(Strictness.LENIENT).create();

    public static PackMcMeta fromNonNullJson(String jsonString) throws JsonParseException {
        PackMcMeta parsed = LENIENT_GSON.fromJson(jsonString, PackMcMeta.class);
        if (parsed == null)
            throw new JsonParseException("Json object cannot be null.");
        return parsed;
    }

    public static PackMcMeta fromNonNullJsonFile(Path jsonFile) throws JsonParseException, IOException {
        try (var reader = Files.newBufferedReader(jsonFile)) {
            PackMcMeta parsed = LENIENT_GSON.fromJson(reader, PackMcMeta.class);
            if (parsed == null)
                throw new JsonParseException("Json object cannot be null.");
            return parsed;
        }
    }

    @Override
    public void validate() throws JsonParseException {
        if (pack == null)
            throw new JsonParseException("pack cannot be null");
    }

View on GitHub (pinned to 24702dc5a0)