PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException(nameof(filterReference))

Error message

ArgumentNullException(nameof(filterReference))

What it means

EventSubscription's constructor requires a non-null filterReference wrapping a Predicate<TPayload>. Prism throws ArgumentNullException when the filter reference itself is null, because a subscription without a filter cannot decide which published payloads to deliver.

Solutions

  1. Pass a valid filter delegate reference, e.g. a Predicate<TPayload> wrapping 'return true;' for accept-all behavior
  2. Ensure the variable holding the filter is initialized before constructing the subscription
  3. If no filtering is needed, call the Subscribe overload without the filter parameter instead of passing null

Example fix

// before
var sub = new EventSubscription<string>(actionRef, null);
// after
var sub = new EventSubscription<string>(actionRef, new DelegateReference((Predicate<string>)(p => true), false));
Defensive patterns

Strategy: validation

Validate before calling

if (filterReference == null)
    throw new ArgumentNullException(nameof(filterReference));
if (filterReference?.Target is not Predicate<TPayload>)
    throw new ArgumentException("filter must be a Predicate<TPayload>", nameof(filterReference));

Type guard

bool IsValidFilter<TPayload>(IDelegateReference r) => r?.Target is Predicate<TPayload>;

Prevention

When it happens

Trigger: Calling new EventSubscription<TPayload>(actionReference, null), or calling PubSubEvent.Subscribe with a null filter delegate that ends up wrapped in a null IDelegateReference.

Common situations: Passing a null filter variable (e.g. a field not yet initialized), or custom subscription wiring code that forgets to supply the filter when Prism's default filter (always true) was expected.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/f4ce784b31fd41bc. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Events/EventSubscription.cs:111

        private readonly IDelegateReference _filterReference;

        ///<summary>
        /// Creates a new instance of <see cref="EventSubscription{TPayload}"/>.
        ///</summary>
        ///<param name="actionReference">A reference to a delegate of type <see cref="System.Action{TPayload}"/>.</param>
        ///<param name="filterReference">A reference to a delegate of type <see cref="Predicate{TPayload}"/>.</param>
        ///<exception cref="ArgumentNullException">When <paramref name="actionReference"/> or <see paramref="filterReference"/> are <see langword="null" />.</exception>
        ///<exception cref="ArgumentException">When the target of <paramref name="actionReference"/> is not of type <see cref="System.Action{TPayload}"/>,
        ///or the target of <paramref name="filterReference"/> is not of type <see cref="Predicate{TPayload}"/>.</exception>
        public EventSubscription(IDelegateReference actionReference, IDelegateReference filterReference)
        {
            if (actionReference == null)
                throw new ArgumentNullException(nameof(actionReference));
            if (!(actionReference.Target is Action<TPayload>))
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Action<TPayload>).FullName), nameof(actionReference));

            if (filterReference == null)
                throw new ArgumentNullException(nameof(filterReference));
            if (!(filterReference.Target is Predicate<TPayload>))
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Predicate<TPayload>).FullName), nameof(filterReference));

            _actionReference = actionReference;
            _filterReference = filterReference;
        }

        /// <summary>
        /// Gets the target <see cref="System.Action{T}"/> that is referenced by the <see cref="IDelegateReference"/>.
        /// </summary>
        /// <value>An <see cref="System.Action{T}"/> or <see langword="null" /> if the referenced target is not alive.</value>
        public Action<TPayload> Action
        {
            get { return (Action<TPayload>)_actionReference.Target; }
        }

        /// <summary>
        /// Gets the target <see cref="Predicate{T}"/> that is referenced by the <see cref="IDelegateReference"/>.

View on GitHub (pinned to 358118cd64)