{"record":{"id":"1c88308c7b16f3a7","repo":"LykosAI/StabilityMatrix","slug":"state-is-null","errorCode":null,"errorMessage":"State is null","messagePattern":"State is null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Models/InferenceProjectDocument.cs","lineNumber":117,"sourceCode":"    public bool TryUpdateModel(string key, Func<JsonNode, JsonNode> modifier)\n    {\n        if (State is not { } state)\n            return false;\n\n        if (!state.TryGetPropertyValue(key, out var modelNode) || modelNode is null)\n        {\n            return false;\n        }\n\n        state[key] = modifier(modelNode);\n\n        return true;\n    }\n\n    public InferenceProjectDocument WithBatchSize(int batchSize, int batchCount)\n    {\n        if (State is null)\n            throw new InvalidOperationException(\"State is null\");\n\n        var document = (InferenceProjectDocument)Clone();\n\n        var batchSizeCard =\n            document.State![\"BatchSize\"] ?? throw new InvalidOperationException(\"BatchSize card is null\");\n\n        batchSizeCard[\"BatchSize\"] = batchSize;\n        batchSizeCard[\"BatchCount\"] = batchCount;\n\n        return document;\n    }\n\n    /// <inheritdoc />\n    public object Clone()\n    {\n        var newObj = (InferenceProjectDocument)MemberwiseClone();\n        // Clone State also since its mutable\n        newObj.State = State == null ? null : JsonSerializer.SerializeToNode(State).Deserialize<JsonObject>();","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Models/InferenceProjectDocument.cs#L99-L135","documentation":"WithBatchSize() clones the project document and edits the BatchSize card inside its State JSON. If the document has no State object (e.g. a new or unloaded document), the method cannot mutate anything and throws InvalidOperationException('State is null').","triggerScenarios":"Calling WithBatchSize(batchSize, batchCount) on an InferenceProjectDocument whose State property is null — typically a freshly created document or one loaded before state initialization.","commonSituations":"Programmatically generating projects without initializing State; loading a corrupt/empty project file; calling batch helpers before the designer view initialized the state graph.","solutions":["Initialize the document State (open/initialize the project so State is populated) before calling WithBatchSize.","Check doc.State is not null before invoking and handle the null case by creating a default state.","Construct documents through the normal project-creation flow that guarantees a State with the required cards."],"exampleFix":"// before\ndoc.WithBatchSize(4, 2); // State may be null\n// after\nif (doc.State is null)\n    doc = InferenceProjectDocument.FromDefaultState();\ndoc = doc.WithBatchSize(4, 2);","handlingStrategy":"validation","validationCode":"if (doc.State is null)\n    doc = doc.WithState(InferenceProjectDocument.DefaultState()); // or initialize via project flow\ndoc = doc.WithBatchSize(batchSize, batchCount);","typeGuard":"bool CanEditState(InferenceProjectDocument d) => d.State is not null;","tryCatchPattern":"try { doc = doc.WithBatchSize(4, 2); }\ncatch (InvalidOperationException ex) when (ex.Message == \"State is null\")\n{ doc = InitializeDefaultProjectDoc(); doc = doc.WithBatchSize(4, 2); }","preventionTips":["Create documents through factory methods that guarantee an initialized State.","Never mutate project documents before the loading/initialization task completes.","Add unit tests covering batch edits on default and loaded documents."],"tags":["null-state","project-document","lifecycle"],"backgroundTag":"null-argument","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}