DapperLib/Dapper · error · ArgumentNullException

type

Error message

type

What it means

QueryAsync(IDbConnection, Type, ...) throws ArgumentNullException(nameof(type)) when the Type argument is null. The non-generic async overload needs a concrete type to materialize rows, so null is unrecoverable. The XML doc explicitly documents this exception. The check happens synchronously before any I/O.

Source

Thrown at Dapper/SqlMapper.Async.cs:215

        [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")]
        public static Task<dynamic?> QuerySingleOrDefaultAsync(this IDbConnection cnn, string sql, object? param = null, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
            QueryRowAsync<dynamic?>(cnn, Row.SingleOrDefault, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default));

        /// <summary>
        /// Execute a query asynchronously using Task.
        /// </summary>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="type">The type to return.</param>
        /// <param name="sql">The SQL to execute for the query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")]
        public static Task<IEnumerable<object>> QueryAsync(this IDbConnection cnn, Type type, string sql, object? param = null, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (type is null) throw new ArgumentNullException(nameof(type));
            return QueryAsync<object>(cnn, type, new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.Buffered, default));
        }

        /// <summary>
        /// Execute a single-row query asynchronously using Task.
        /// </summary>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="type">The type to return.</param>
        /// <param name="sql">The SQL to execute for the query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")]
        public static Task<object> QueryFirstAsync(this IDbConnection cnn, Type type, string sql, object? param = null, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (type is null) throw new ArgumentNullException(nameof(type));

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Resolve and null-check the Type before calling QueryAsync.
  2. Correct the assembly-qualified type name and ensure the assembly is loaded.

Example fix

// before
var rows = await cnn.QueryAsync(Type.GetType(modelName), sql, param);

// after
var type = Type.GetType(modelName) ?? throw new InvalidOperationException($"Unknown type: {modelName}");
var rows = await cnn.QueryAsync(type, sql, param);
Defensive patterns

Strategy: validation

Validate before calling

var type = Type.GetType(modelName) ?? throw new InvalidOperationException($"Unknown type: {modelName}");
var rows = await cnn.QueryAsync(type, sql, param);

Type guard

static Type RequireType(Type? t) => t ?? throw new ArgumentNullException("type");

Prevention

When it happens

Trigger: Calling cnn.QueryAsync(null, sql, ...) — e.g. passing a type resolved via Type.GetType that returned null; passing a typeof expression replaced by a variable that lost its value.

Common situations: Type.GetType with a wrong assembly-qualified name returning null; deserializing into a type chosen from config where the key was missing; generics erased to object where the type slot became null.

Related errors


AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13). Data as JSON: /api/errors/474147b42873cace. Report an issue: GitHub.