{"record":{"id":"59bd38266b9aea09","repo":"microsoft/aspire","slug":"object-reference-not-passed-for-parameter-args","errorCode":null,"errorMessage":"Object reference not passed for parameter 'args' (ArgumentNullException: args)","messagePattern":"Object reference not passed for parameter 'args' \\(ArgumentNullException: args\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Rust/RustCargoArgsCallbackAnnotation.cs","lineNumber":61,"sourceCode":"/// starts, so there is nothing for a deferred value such as an endpoint reference to resolve against;\n/// those belong after the <c>--</c> separator and are added with <c>WithArgs</c>.\n/// </remarks>\npublic sealed class RustCargoArgsCallbackContext(RustAppResource resource, IList<string> args, CancellationToken cancellationToken = default)\n{\n    /// <summary>\n    /// Gets the Rust application resource whose cargo arguments are being built.\n    /// </summary>\n    /// <remarks>\n    /// The same callbacks run for both the local <c>cargo run</c> command line and the generated\n    /// Dockerfile, so a callback that needs to know which resource it is configuring — or that needs to\n    /// read annotations placed on it — reads them from here rather than capturing the resource itself.\n    /// </remarks>\n    public RustAppResource Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));\n\n    /// <summary>\n    /// Gets the list of command-line arguments.\n    /// </summary>\n    public IList<string> Args { get; } = args ?? throw new ArgumentNullException(nameof(args));\n\n    /// <summary>\n    /// Gets the cancellation token associated with the callback context.\n    /// </summary>\n    public CancellationToken CancellationToken { get; } = cancellationToken;\n}\n","sourceCodeStart":43,"sourceCodeEnd":68,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Rust/RustCargoArgsCallbackAnnotation.cs#L43-L68","documentation":"RustCargoArgsCallbackContext's constructor guards its 'args' parameter with 'args ?? throw new ArgumentNullException(nameof(args))'. The Args list is the mutable output the callback populates with cargo-level command-line arguments, so it must always be a live list instance. Passing null is treated as a caller bug and rejected immediately at construction.","triggerScenarios":"Constructing RustCargoArgsCallbackContext directly with a null IList<string> for args — e.g. a field that was never initialized, a method returning null on some path, or a misordered constructor call passing the wrong variable.","commonSituations":"Unit tests building callback contexts by hand; custom pipelines assembling the context where the argument list comes from a nullable source; refactoring that swapped the resource and args parameters.","solutions":["Pass a real list instance, e.g. new List<string>(), for the args parameter","Initialize the argument-list field/property before constructing the context","Fix any producer method so it returns an empty list rather than null"],"exampleFix":"// before\nIList<string>? args = config.GetArgs(); // may return null\nvar context = new RustCargoArgsCallbackContext(resource, args, ct);\n// after\nIList<string> args = config.GetArgs() ?? new List<string>();\nvar context = new RustCargoArgsCallbackContext(resource, args, ct);","handlingStrategy":"type-guard","validationCode":"if (args is null) throw new InvalidOperationException(\"Callback context requires a non-null args list.\");","typeGuard":"bool HasArgsList(IList<string>? a) => a is not null;","tryCatchPattern":"try { var ctx = new RustCargoArgsCallbackContext(resource, args, ct); } catch (ArgumentNullException ex) when (ex.ParamName == \"args\") { log.LogError(ex, \"Null args list for callback context\"); }","preventionTips":["Default nullable list producers to an empty list with ?? new List<string>()","Keep constructor argument order (resource, args, token) consistent to avoid passing the wrong variable","Initialize argument-list fields at declaration"],"tags":["argument-null","rust","callback-context"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}