{"record":{"id":"27cb48211c91e0e3","repo":"microsoft/semantic-kernel","slug":"source-step-cannot-be-null","errorCode":null,"errorMessage":"Source step cannot be null.","messagePattern":"Source step cannot be null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Experimental/Process.Core/ListenForTargetBuilder.cs","lineNumber":87,"sourceCode":"    internal ListenForTargetBuilder EmitEvent(string eventName, Dictionary<string, string>? payload = null)\n    {\n        Verify.NotNullOrWhiteSpace(eventName, nameof(eventName));\n        this.SendEventTo_Internal(new ProcessEmitTargetBuilder(eventName, payload));\n        return new ListenForTargetBuilder(this._messageSources, this._processBuilder, this.EdgeGroupBuilder);\n    }\n\n    /// <summary>\n    /// Sends the event to the specified target.\n    /// </summary>\n    /// <param name=\"target\">The target to send the event to.</param>\n    /// <returns>A new instance of <see cref=\"ListenForTargetBuilder\"/>.</returns>\n    internal override ProcessStepEdgeBuilder SendEventTo_Internal(ProcessTargetBuilder target)\n    {\n        foreach (var messageSource in this._messageSources)\n        {\n            if (messageSource.Source == null)\n            {\n                throw new InvalidOperationException(\"Source step cannot be null.\");\n            }\n\n            // Link all the source steps to the event listener\n            var onEventBuilder = messageSource.Source.OnEvent(messageSource.MessageType);\n            onEventBuilder.EdgeGroupBuilder = this.EdgeGroupBuilder;\n\n            if (messageSource.Condition != null)\n            {\n                onEventBuilder.Condition = messageSource.Condition;\n            }\n            onEventBuilder.SendEventTo(target);\n        }\n\n        return new ListenForTargetBuilder(this._messageSources, this._processBuilder, edgeGroup: this.EdgeGroupBuilder);\n    }\n\n    /// <summary>\n    /// Signals that the process should be stopped.","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Experimental/Process.Core/ListenForTargetBuilder.cs#L69-L105","documentation":"Thrown by ListenForTargetBuilder.SendEventTo_Internal when iterating over _messageSources and finding a MessageSourceBuilder whose Source property is null. The ListenFor pattern requires every message source to reference a concrete source step so it can wire up an OnEvent edge.","triggerScenarios":"A MessageSourceBuilder in the _messageSources list has a null Source, meaning the originating step was never set or was lost during builder cloning/copying. This occurs when ListenFor().Message(...) is called with a source step that was not yet built or was set to null.","commonSituations":"Using the internal ListenForBuilder API to define complex multi-source listeners where one source step is still null (not yet resolved). Copying or reusing a ListenForTargetBuilder whose message sources include a dangling reference. Building a process programmatically where a step is added to a listener before the step itself is fully constructed.","solutions":["Ensure every source step passed into ListenFor/Message is a fully constructed ProcessStepBuilder with a non-null reference before calling SendEventTo.","Debug the _messageSources list at the point of the call to identify which MessageSourceBuilder has a null Source and fix or remove it.","Avoid creating MessageSourceBuilder instances directly; use the public ListenFor API which validates source steps."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Validate message sources before calling SendEventTo on a ListenForTargetBuilder.\n// Ensure all MessageSourceBuilder instances have non-null Source.\npublic bool ValidateSources(List<MessageSourceBuilder> sources)\n{\n    return sources.All(s => s.Source is not null);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the public ProcessBuilder.ListenFor() API rather than internal builders to ensure source validation.","Never create MessageSourceBuilder with a null Source step.","Fully construct source steps before adding them to a listener."],"tags":["semantic-kernel","process-framework","listen-for","builder","null-reference"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}