dotnet/maui · error · ArgumentNullException
_ = view ?? throw new ArgumentNullException(nameof(view));
Error message
_ = view ?? throw new ArgumentNullException(nameof(view));
What it means
DropEventArgs's public constructor throws ArgumentNullException(nameof(view)) when the DataPackageView argument is null. The constructor is the documented entry point for Drop events; a null data package is treated as a programming error. (An internal overload tolerates a null view and substitutes a fresh DataPackageView.)
Source
Thrown at src/Controls/src/Core/DragAndDrop/DropEventArgs.cs:19
using System;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls
{
/// <summary>
/// Provides data for the <see cref="DropGestureRecognizer.Drop"/> event.
/// </summary>
public class DropEventArgs
{
Func<IElement?, Point?>? _getPosition;
/// <summary>
/// Initializes a new instance of the <see cref="DropEventArgs"/> class.
/// </summary>
/// <param name="view">The data package associated with the drop.</param>
public DropEventArgs(DataPackageView view)
{
_ = view ?? throw new ArgumentNullException(nameof(view));
Data = view;
}
/// <summary>
/// Initializes a new instance of the <see cref="DropEventArgs"/> class.
/// </summary>
/// <param name="view">The data package associated with the drop.</param>
/// <param name="getPosition">Function used to get the position relative a specified <see cref="IElement"/>.</param>
/// <param name="platformArgs">The platform-specific data associated with the drag.</param>
internal DropEventArgs(DataPackageView? view, Func<IElement?, Point?>? getPosition, PlatformDropEventArgs platformArgs)
{
Data = view ?? new DataPackageView(new DataPackage());
_getPosition = getPosition;
PlatformArgs = platformArgs;
}
/// <summary>
/// Gets the data package associated with the drop event.View on GitHub (pinned to f377ff1c5e)
Solutions
- Pass a non-null DataPackageView: wrap a DataPackage with `new DataPackageView(new DataPackage())` if empty.
- Use the internal overload only if you are framework/platform code and intentionally allow a default package.
- Null-check before constructing and supply an empty package.
Example fix
// before var args = new DropEventArgs(null); // throws // after var args = new DropEventArgs(new DataPackageView(new DataPackage()));
Defensive patterns
Strategy: validation
Validate before calling
static DropEventArgs MakeDropArgs(DataPackageView? view) =>
new(view ?? new DataPackageView(new DataPackage())); Type guard
static bool HasDataPackage(DataPackageView? v) => v is not null;
Prevention
- Never call the public DropEventArgs ctor with null.
- Keep an empty DataPackageView handy: new DataPackageView(new DataPackage()).
- Null-check before constructing and supply an empty package.
When it happens
Trigger: Constructing `new DropEventArgs(null)` via the public constructor; event-raising code that passes a null DataPackageView.
Common situations: Test code raising Drop events without a data package; refactoring that loses the DataPackageView reference before constructing args.
Related errors
- _ = args ?? throw new ArgumentNullException(nameof(args));
- public ElementEventArgs(Element element) => Element = elemen
- 'selector' cannot be null or whitespace.
- newElement
- ContentLoader
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/12a95b1f138ea2a6.
Report an issue: GitHub.