DapperLib/Dapper · error · ArgumentNullException

type

Error message

type

What it means

GridReader.Read(Type type, bool buffered) is the synchronous non-generic overload that reads the next grid into instances of a runtime Type, throwing ArgumentNullException(nameof(type)) at SqlMapper.GridReader.cs:130 when type is null. The generic Read<T> overload cannot hit this.

Source

Thrown at Dapper/SqlMapper.GridReader.cs:130

            /// </summary>
            /// <typeparam name="T">The type to read.</typeparam>
            public T ReadSingle<T>() => ReadRow<T>(typeof(T), Row.Single);

            /// <summary>
            /// Read an individual row of the next grid of results.
            /// </summary>
            /// <typeparam name="T">The type to read.</typeparam>
            public T? ReadSingleOrDefault<T>() => ReadRow<T>(typeof(T), Row.SingleOrDefault);

            /// <summary>
            /// Read the next grid of results.
            /// </summary>
            /// <param name="type">The type to read.</param>
            /// <param name="buffered">Whether to buffer the results.</param>
            /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
            public IEnumerable<object> Read(Type type, bool buffered = true)
            {
                if (type is null) throw new ArgumentNullException(nameof(type));
                return ReadImpl<object>(type, buffered);
            }

            /// <summary>
            /// Read an individual row of the next grid of results.
            /// </summary>
            /// <param name="type">The type to read.</param>
            /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
            public object ReadFirst(Type type)
            {
                if (type is null) throw new ArgumentNullException(nameof(type));
                return ReadRow<object>(type, Row.First);
            }

            /// <summary>
            /// Read an individual row of the next grid of results.
            /// </summary>
            /// <param name="type">The type to read.</param>

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Null-check the Type before calling and surface the unresolved name.
  2. Use the generic Read<T>() overload to make the error a compile-time one.
  3. Centralize Type resolution in a helper that throws on null.

Example fix

// before
Type t = Type.GetType(name); // null if unresolved
var rows = grid.Read(t);

// after
Type t = Type.GetType(name) ?? throw new InvalidOperationException($"Unresolved type {name}");
var rows = grid.Read(t);
Defensive patterns

Strategy: validation

Validate before calling

if (type is null) throw new InvalidOperationException("type must be resolved before reading a grid");
var rows = grid.Read(type);

Type guard

Type t = Type.GetType(name) ?? throw new InvalidOperationException($"Unresolved type {name}");

Prevention

When it happens

Trigger: Call grid.Read((Type)null) — usually a Type variable from reflection or a lookup that returned null.

Common situations: Type.GetType returning null for an unresolvable assembly-qualified name; dispatch tables with a missing mapping; conditional Type selection without an else.

Related errors


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