LykosAI/StabilityMatrix · error · InvalidOperationException

State is null

Error message

State is null

What it means

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').

Solutions

  1. Initialize the document State (open/initialize the project so State is populated) before calling WithBatchSize.
  2. Check doc.State is not null before invoking and handle the null case by creating a default state.
  3. Construct documents through the normal project-creation flow that guarantees a State with the required cards.

Example fix

// before
doc.WithBatchSize(4, 2); // State may be null
// after
if (doc.State is null)
    doc = InferenceProjectDocument.FromDefaultState();
doc = doc.WithBatchSize(4, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (doc.State is null)
    doc = doc.WithState(InferenceProjectDocument.DefaultState()); // or initialize via project flow
doc = doc.WithBatchSize(batchSize, batchCount);

Type guard

bool CanEditState(InferenceProjectDocument d) => d.State is not null;

Try / catch

try { doc = doc.WithBatchSize(4, 2); }
catch (InvalidOperationException ex) when (ex.Message == "State is null")
{ doc = InitializeDefaultProjectDoc(); doc = doc.WithBatchSize(4, 2); }

Prevention

When it happens

Trigger: Calling WithBatchSize(batchSize, batchCount) on an InferenceProjectDocument whose State property is null — typically a freshly created document or one loaded before state initialization.

Common situations: Programmatically generating projects without initializing State; loading a corrupt/empty project file; calling batch helpers before the designer view initialized the state graph.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/1c88308c7b16f3a7. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Models/InferenceProjectDocument.cs:117

    public bool TryUpdateModel(string key, Func<JsonNode, JsonNode> modifier)
    {
        if (State is not { } state)
            return false;

        if (!state.TryGetPropertyValue(key, out var modelNode) || modelNode is null)
        {
            return false;
        }

        state[key] = modifier(modelNode);

        return true;
    }

    public InferenceProjectDocument WithBatchSize(int batchSize, int batchCount)
    {
        if (State is null)
            throw new InvalidOperationException("State is null");

        var document = (InferenceProjectDocument)Clone();

        var batchSizeCard =
            document.State!["BatchSize"] ?? throw new InvalidOperationException("BatchSize card is null");

        batchSizeCard["BatchSize"] = batchSize;
        batchSizeCard["BatchCount"] = batchCount;

        return document;
    }

    /// <inheritdoc />
    public object Clone()
    {
        var newObj = (InferenceProjectDocument)MemberwiseClone();
        // Clone State also since its mutable
        newObj.State = State == null ? null : JsonSerializer.SerializeToNode(State).Deserialize<JsonObject>();

View on GitHub (pinned to af93d6ef57)