{"record":{"id":"9ebd116b6c7738f1","repo":"Devolutions/UniGetUI","slug":"jsonnode-inc-pkg-was-null-when-it-shouldn-t","errorCode":null,"errorMessage":"JsonNode? inc_pkg was null, when it shouldn't","messagePattern":"JsonNode\\? inc_pkg was null, when it shouldn't","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/UniGetUI.PackageEngine.Serializable/SerializableBundle.cs","lineNumber":59,"sourceCode":"        public override void LoadFromJson(JsonNode data)\n        {\n            this.export_version = data[nameof(export_version)]?.GetVal<double>() ?? 0;\n            this.incompatible_packages_info =\n                data[nameof(incompatible_packages_info)]?.GetVal<string>() ?? IncompatMessage;\n            this.packages = new List<SerializablePackage>();\n            this.incompatible_packages = new List<SerializableIncompatiblePackage>();\n\n            foreach (JsonNode? pkg in data[nameof(packages)]?.AsArray2() ?? new())\n            {\n                if (pkg is null)\n                    throw new InvalidDataException(\"JsonNode? pkg was null, when it shouldn't\");\n                packages.Add(new SerializablePackage(pkg));\n            }\n\n            foreach (JsonNode? inc_pkg in data[nameof(incompatible_packages)]?.AsArray2() ?? new())\n            {\n                if (inc_pkg is null)\n                    throw new InvalidDataException(\"JsonNode? inc_pkg was null, when it shouldn't\");\n                incompatible_packages.Add(new SerializableIncompatiblePackage(inc_pkg));\n            }\n        }\n\n        public override JsonObject AsJsonNode()\n        {\n            JsonObject obj = new();\n            obj.Add(nameof(export_version), export_version);\n            obj.Add(\n                nameof(packages),\n                new JsonArray(packages.Select(p => p.AsJsonNode()).ToArray())\n            );\n            obj.Add(nameof(incompatible_packages_info), incompatible_packages_info);\n            obj.Add(\n                nameof(incompatible_packages),\n                new JsonArray(incompatible_packages.Select(p => p.AsJsonNode()).ToArray())\n            );\n            return obj;","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/Devolutions/UniGetUI/blob/9b1d7d0eab91620fc15c86b36676532095936ae3/src/UniGetUI.PackageEngine.Serializable/SerializableBundle.cs#L41-L77","documentation":"Same loop as error 102 but for the 'incompatible_packages' array. SerializableBundle.LoadFromJson iterates JsonNode? elements from data[\"incompatible_packages\"]?.AsArray2(); any JSON `null` literal element makes the throw fire with message 'JsonNode? inc_pkg was null, when it shouldn't'. The exception propagates uncaught out of LoadFromJson, aborting the bundle load.","triggerScenarios":"Calling LoadFromJson(node) / `new SerializableBundle(node)` where node[\"incompatible_packages\"] is a JSON array containing one or more null elements. Note the array may be entirely absent (then `?? new()` yields an empty array and no throw), so the failure specifically requires a present array with null members.","commonSituations":"Bundle exports containing 'incompatible packages' (packages from a local source or an unavailable manager) where the exporter wrote a null placeholder; truncated or hand-edited export files; imports across UniGetUI versions with differing incompatible-package schemas.","solutions":["Regenerate the bundle export so incompatible-packages entries are well-formed.","Pre-clean the 'incompatible_packages' array to strip null elements before import.","Confirm export_version == 3 (SerializableBundle.ExpectedVersion) to rule out a format mismatch.","Relax the guard to `if (inc_pkg is null) continue;` if null slots should be tolerated rather than aborting the whole import.","Open the JSON and inspect the incompatible_packages array indices that are null."],"exampleFix":"// before (SerializableBundle.cs:56-61)\nforeach (JsonNode? inc_pkg in data[nameof(incompatible_packages)]?.AsArray2() ?? new())\n{\n    if (inc_pkg is null)\n        throw new InvalidDataException(\"JsonNode? inc_pkg was null, when it shouldn't\");\n    incompatible_packages.Add(new SerializableIncompatiblePackage(inc_pkg));\n}\n\n// after — tolerate corrupt null slots\nforeach (JsonNode? inc_pkg in data[nameof(incompatible_packages)]?.AsArray2() ?? new())\n{\n    if (inc_pkg is null) continue;\n    incompatible_packages.Add(new SerializableIncompatiblePackage(inc_pkg));\n}","handlingStrategy":"validation","validationCode":"// Strip null elements from the 'incompatible_packages' array before deserializing.\nstatic JsonNode SanitizeBundle(JsonNode node)\n{\n    if (node is JsonObject obj && obj[\"incompatible_packages\"] is JsonArray inc)\n    {\n        JsonArray clean = new();\n        foreach (var p in inc.Where(n => n is not null))\n            clean.Add(p.DeepClone());\n        obj[\"incompatible_packages\"] = clean;\n    }\n    return node;\n}\n\nvar safe = SanitizeBundle(JsonNode.Parse(File.ReadAllText(path))!);\nvar bundle = new SerializableBundle(safe);","typeGuard":"static bool BundleIncompatHaveNoNulls(JsonNode node)\n{\n    if (node is not JsonObject obj) return false;\n    if (obj[\"incompatible_packages\"] is not JsonArray arr) return true;\n    return arr.All(e => e is not null);\n}","tryCatchPattern":"try\n{\n    var bundle = new SerializableBundle(JsonNode.Parse(json)!);\n}\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"inc_pkg was null\"))\n{\n    Logger.Error(\"Bundle file has null incompatible-package entries; refusing to import corrupt file.\");\n}","preventionTips":["Inspect the 'incompatible_packages' array directly for null slots before import — its entries are optional and sparse in some exporters.","Regenerate the bundle rather than patching null entries by hand.","Confirm export_version == 3 to rule out schema drift for incompatible packages.","If your workflow legitimately has gaps, filter nulls upstream so LoadFromJson never sees them."],"tags":["json","deserialization","data-validation","import"],"backgroundTag":null,"analyzedSha":"9b1d7d0eab91620fc15c86b36676532095936ae3","analyzedAt":"2026-08-13T12:19:18.278Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}