{"record":{"id":"0384c0d69bcfbffa","repo":"LuckyPennySoftware/AutoMapper","slug":"sourceexpression-may-not-be-null-when-mapping-des","errorCode":null,"errorMessage":"sourceExpression may not be null when mapping {DestinationMember.Name} from {typeof(TSource)} to {typeof(TDestination)}.","messagePattern":"sourceExpression may not be null when mapping (.+?) from (.+?) to (.+?)\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/Configuration/PathConfigurationExpression.cs","lineNumber":35,"sourceCode":"    /// <summary>\n    /// Ignore this member for configuration validation and skip during mapping\n    /// </summary>\n    void Ignore();\n    void Condition(Func<ConditionParameters<TSource, TDestination, TMember>, bool> condition);\n}\npublic readonly record struct ConditionParameters<TSource, TDestination, TMember>(TSource Source, TDestination Destination, TMember SourceMember, TMember DestinationMember, ResolutionContext Context);\npublic sealed class PathConfigurationExpression<TSource, TDestination, TMember>(LambdaExpression destinationExpression, Stack<Member> chain) : IPathConfigurationExpression<TSource, TDestination, TMember>, IPropertyMapConfiguration\n{\n    private readonly LambdaExpression _destinationExpression = destinationExpression;\n    private LambdaExpression _sourceExpression;\n    List<Action<PathMap>> PathMapActions { get; } = [];\n    public MemberPath MemberPath { get; } = new(chain);\n    public MemberInfo DestinationMember => MemberPath.Last;\n    public void MapFrom<TSourceMember>(Expression<Func<TSource, TSourceMember>> sourceExpression) => MapFromUntyped(sourceExpression);\n    public void Ignore() => PathMapActions.Add(pm => pm.Ignored = true);\n    public void MapFromUntyped(LambdaExpression sourceExpression)\n    {\n        _sourceExpression = sourceExpression ?? throw new System.ArgumentNullException(nameof(sourceExpression), $\"{nameof(sourceExpression)} may not be null when mapping {DestinationMember.Name} from {typeof(TSource)} to {typeof(TDestination)}.\");\n        PathMapActions.Add(pm => pm.MapFrom(sourceExpression));\n    }\n    public void Configure(TypeMap typeMap)\n    {\n        var pathMap = typeMap.FindOrCreatePathMapFor(_destinationExpression, MemberPath, typeMap);\n        Apply(pathMap);\n    }\n    private void Apply(PathMap pathMap)\n    {\n        foreach (var action in PathMapActions)\n        {\n            action(pathMap);\n        }\n    }\n    internal static IPropertyMapConfiguration Create(LambdaExpression destination, LambdaExpression source)\n    {\n        if (destination == null || !destination.IsMemberPath(out var chain))\n        {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/Configuration/PathConfigurationExpression.cs#L17-L53","documentation":"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.","triggerScenarios":"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.","commonSituations":"Refactoring that leaves a null MapFrom; conditional ternary that produces null; passing an uninitialized expression field.","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."],"exampleFix":"// before — null expression passed to MapFrom\nExpression<Func<Source, string>> expr = GetExpr(); // may return null\nCreateMap<Source, Dest>().ForPath(d => d.Addr.City, opt => opt.MapFrom(expr));\n\n// after — guard against null\nExpression<Func<Source, string>> expr = GetExpr() ?? (s => s.DefaultCity);\nCreateMap<Source, Dest>().ForPath(d => d.Addr.City, opt => opt.MapFrom(expr));","handlingStrategy":"validation","validationCode":"// Guard against null before passing to MapFrom in ForPath\nExpression<Func<Source, TMember>> expr = GetSourceExpression();\nif (expr == null)\n    throw new ArgumentNullException(nameof(expr), \"MapFrom expression must not be null in ForPath.\");\nCreateMap<Source, Dest>().ForPath(d => d.Child.Prop, opt => opt.MapFrom(expr));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["configuration","forpath","null-argument","mapping"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}