{"record":{"id":"0fdee5e9ba14aaae","repo":"PrismLibrary/Prism","slug":"argumentnullexception-delegate","errorCode":null,"errorMessage":"ArgumentNullException(\"delegate\")","messagePattern":"ArgumentNullException\\(\"delegate\"\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Events/DelegateReference.cs","lineNumber":30,"sourceCode":"    /// internally by the Prism Library.\n    /// </summary>\n    public class DelegateReference : IDelegateReference\n    {\n        private readonly Delegate _delegate;\n        private readonly WeakReference _weakReference;\n        private readonly MethodInfo _method;\n        private readonly Type _delegateType;\n\n        /// <summary>\n        /// Initializes a new instance of <see cref=\"DelegateReference\"/>.\n        /// </summary>\n        /// <param name=\"delegate\">The original <see cref=\"Delegate\"/> to create a reference for.</param>\n        /// <param name=\"keepReferenceAlive\">If <see langword=\"false\" /> the class will create a weak reference to the delegate, allowing it to be garbage collected. Otherwise it will keep a strong reference to the target.</param>\n        /// <exception cref=\"ArgumentNullException\">If the passed <paramref name=\"delegate\"/> is not assignable to <see cref=\"Delegate\"/>.</exception>\n        public DelegateReference(Delegate @delegate, bool keepReferenceAlive)\n        {\n            if (@delegate == null)\n                throw new ArgumentNullException(\"delegate\");\n\n            if (keepReferenceAlive)\n            {\n                this._delegate = @delegate;\n            }\n            else\n            {\n                _weakReference = new WeakReference(@delegate.Target);\n                _method = @delegate.GetMethodInfo();\n                _delegateType = @delegate.GetType();\n            }\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"Delegate\" /> (the target) referenced by the current <see cref=\"DelegateReference\"/> object.\n        /// </summary>\n        /// <value><see langword=\"null\"/> if the object referenced by the current <see cref=\"DelegateReference\"/> object has been garbage collected; otherwise, a reference to the <see cref=\"Delegate\"/> referenced by the current <see cref=\"DelegateReference\"/> object.</value>\n        public Delegate Target","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Events/DelegateReference.cs#L12-L48","documentation":"DelegateReference wraps a delegate as a strong or weak reference for Prism's event aggregator. Passing a null Delegate to the constructor throws ArgumentNullException with the literal name \"delegate\". A non-null delegate is mandatory because the reference has nothing to wrap otherwise.","triggerScenarios":"Calling new DelegateReference(null, keepReferenceAlive) directly, or via PubSubEvent.Subscribe(null, ...) / filter/action delegates resolved to null at runtime (e.g. a delegate field that is null).","commonSituations":"Subscribing to an EventAggregator event with an uninitialized method-group or Func field; passing result of a factory method that returned null; older Prism versions where the parameter is named \"delegate\" so the exception message shows \"delegate\" instead of @delegate.","solutions":["Pass a non-null delegate to DelegateReference / Subscribe","Check the delegate variable for null before subscribing","Use nameof-safe subscription with a concrete method group instead of a nullable delegate field"],"exampleFix":"// before\nAction<MyPayload> handler = _handler; // may be null\n_event.Subscribe(handler);\n// after\nif (_handler != null) _event.Subscribe(_handler);","handlingStrategy":"validation","validationCode":"if (handler == null) throw new InvalidOperationException(\"Cannot subscribe with a null delegate\");\n_event.Subscribe(handler);","typeGuard":"bool IsSubscribable(Delegate d) => d != null;","tryCatchPattern":"try { _event.Subscribe(handler); }\ncatch (ArgumentNullException) { log.Error(\"Subscribe called with null delegate\"); }","preventionTips":["Subscribe with method groups or lambda expressions, not nullable delegate fields","Null-check delegate fields before subscribing","Prefer keepReferenceAlive:true if subscriber lifetime is uncertain"],"tags":["prism","eventaggregator","argumentnull","delegate"],"backgroundTag":"null-argument","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}