LuckyPennySoftware/AutoMapper · error · ArgumentNullException
sourceExpression may not be null when mapping {DestinationMe
Error message
sourceExpression may not be null when mapping {DestinationMember.Name} from {typeof(TSource)} to {typeof(TDestination)}. What it means
Thrown by MapFromUntyped in PathConfigurationExpression when a null lambda expression is passed as the source expression during ForPath configuration. The null check produces an ArgumentNullException with a message identifying the destination member, source type, and destination type. This is a programmer error, not a configuration validation issue.
Source
Thrown at src/AutoMapper/Configuration/PathConfigurationExpression.cs:35
/// <summary>
/// Ignore this member for configuration validation and skip during mapping
/// </summary>
void Ignore();
void Condition(Func<ConditionParameters<TSource, TDestination, TMember>, bool> condition);
}
public readonly record struct ConditionParameters<TSource, TDestination, TMember>(TSource Source, TDestination Destination, TMember SourceMember, TMember DestinationMember, ResolutionContext Context);
public sealed class PathConfigurationExpression<TSource, TDestination, TMember>(LambdaExpression destinationExpression, Stack<Member> chain) : IPathConfigurationExpression<TSource, TDestination, TMember>, IPropertyMapConfiguration
{
private readonly LambdaExpression _destinationExpression = destinationExpression;
private LambdaExpression _sourceExpression;
List<Action<PathMap>> PathMapActions { get; } = [];
public MemberPath MemberPath { get; } = new(chain);
public MemberInfo DestinationMember => MemberPath.Last;
public void MapFrom<TSourceMember>(Expression<Func<TSource, TSourceMember>> sourceExpression) => MapFromUntyped(sourceExpression);
public void Ignore() => PathMapActions.Add(pm => pm.Ignored = true);
public void MapFromUntyped(LambdaExpression sourceExpression)
{
_sourceExpression = sourceExpression ?? throw new System.ArgumentNullException(nameof(sourceExpression), $"{nameof(sourceExpression)} may not be null when mapping {DestinationMember.Name} from {typeof(TSource)} to {typeof(TDestination)}.");
PathMapActions.Add(pm => pm.MapFrom(sourceExpression));
}
public void Configure(TypeMap typeMap)
{
var pathMap = typeMap.FindOrCreatePathMapFor(_destinationExpression, MemberPath, typeMap);
Apply(pathMap);
}
private void Apply(PathMap pathMap)
{
foreach (var action in PathMapActions)
{
action(pathMap);
}
}
internal static IPropertyMapConfiguration Create(LambdaExpression destination, LambdaExpression source)
{
if (destination == null || !destination.IsMemberPath(out var chain))
{View on GitHub (pinned to dfa6dd587c)
Solutions
- Always pass a non-null lambda expression to MapFrom inside ForPath.
- If mapping is conditional, branch before calling MapFrom rather than passing null.
- Use a simple identity lambda (s => default) as a placeholder if you need to defer the real mapping.
Example fix
// before — null expression passed to MapFrom Expression<Func<Source, string>> expr = GetExpr(); // may return null CreateMap<Source, Dest>().ForPath(d => d.Addr.City, opt => opt.MapFrom(expr)); // after — guard against null Expression<Func<Source, string>> expr = GetExpr() ?? (s => s.DefaultCity); CreateMap<Source, Dest>().ForPath(d => d.Addr.City, opt => opt.MapFrom(expr));
Defensive patterns
Strategy: validation
Validate before calling
// Guard against null before passing to MapFrom in ForPath
Expression<Func<Source, TMember>> expr = GetSourceExpression();
if (expr == null)
throw new ArgumentNullException(nameof(expr), "MapFrom expression must not be null in ForPath.");
CreateMap<Source, Dest>().ForPath(d => d.Child.Prop, opt => opt.MapFrom(expr)); Prevention
- Never pass null to MapFrom inside ForPath — always provide a valid lambda.
- If the source expression is conditionally built, ensure a non-null fallback.
- Enable nullable reference types to get compile-time warnings for potential nulls.
When it happens
Trigger: Calling .MapFrom((Expression<Func<Source, T>>)null) inside a ForPath callback, or passing a null-valued expression variable to MapFrom. This can happen when conditional code accidentally evaluates to a null expression.
Common situations: Refactoring that leaves a null MapFrom; conditional ternary that produces null; passing an uninitialized expression field.
Related errors
- Only member accesses are allowed. {destinationMember}
- The type {typeMap.DestinationType.Name} does not have a cons
- Type '{conditionType.Name}' does not implement ICondition<TS
- Type '{conditionType.Name}' does not implement IPreCondition
- As must specify a derived type, not {DestinationType}
AI-assisted analysis of LuckyPennySoftware/AutoMapper@dfa6dd587c (2026-08-13).
Data as JSON: /api/errors/0384c0d69bcfbffa.
Report an issue: GitHub.