dotnet/machinelearning · error · ArgumentNullException

Strings.InconsistentNullBitMapAndNullCount

Error message

Strings.InconsistentNullBitMapAndNullCount

What it means

This constructor builds a column from a raw value buffer plus a null bit-map; when the bit-map is empty (IsEmpty) the column must have nullCount == 0, otherwise it throws ArgumentNullException(Strings.InconsistentNullBitMapAndNullCount, nameof(nullBitMap)). 'No bit-map but non-zero null count' is an unverifiable state, so the library refuses it.

Source

Thrown at src/Microsoft.Data.Analysis/PrimitiveColumnContainer.cs:68

            {
                DataFrameBuffer<T> mutableBuffer = new DataFrameBuffer<T>(length);
                mutableBuffer.IncreaseSize(length);
                mutableBuffer.RawSpan.Fill(default(T));
                dataBuffer = mutableBuffer;
            }
            else
            {
                dataBuffer = new ReadOnlyDataFrameBuffer<T>(buffer, length);
            }
            Buffers.Add(dataBuffer);

            int bitMapBufferLength = (length + 7) / 8;
            ReadOnlyDataFrameBuffer<byte> nullDataFrameBuffer;
            if (nullBitMap.IsEmpty)
            {
                if (nullCount != 0)
                {
                    throw new ArgumentNullException(Strings.InconsistentNullBitMapAndNullCount, nameof(nullBitMap));
                }
                if (!buffer.IsEmpty)
                {
                    // Create a new bitMap with all the bits up to length set
                    var bitMap = new DataFrameBuffer<byte>(bitMapBufferLength);
                    bitMap.IncreaseSize(bitMapBufferLength);

                    var span = bitMap.Span;
                    span.Fill(255);
                    int lastByte = 1 << (length - (bitMapBufferLength - 1) * 8);
                    span[bitMapBufferLength - 1] = (byte)(lastByte - 1);

                    nullDataFrameBuffer = bitMap;
                }
                else
                {
                    nullDataFrameBuffer = new DataFrameBuffer<byte>();
                }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Provide a real null bit-map buffer when nullCount > 0 (one bit per value, set bit = valid).
  2. If there are no nulls, pass nullCount: 0 alongside the empty bit-map.
  3. Compute nullCount from the bit-map instead of passing a stale value.
  4. Only omit the bit-map when data is guaranteed all-valid.

Example fix

// before
new PrimitiveColumnContainer<int>(buffer, Memory<byte>.Empty, nullCount: 3); // throws
// after
var bitMap = BuildBitMap(buffer.Length, nullIndices); // set bits for valid rows
new PrimitiveColumnContainer<int>(buffer, bitMap, nullCount: 3);
Defensive patterns

Strategy: validation

Validate before calling

bool consistent = !nullBitMap.IsEmpty || nullCount == 0;
if (!consistent) throw new ArgumentException("Empty null bit-map requires nullCount == 0");

Type guard

bool isConsistentNullState(ReadOnlyMemory<byte> nullBitMap, long nullCount) =>
    !nullBitMap.IsEmpty || nullCount == 0;

Try / catch

try { var c = new PrimitiveColumnContainer<int>(buffer, nullBitMap, nullCount); }
catch (ArgumentNullException ex) when (ex.ParamName == "nullBitMap")
{ /* bit-map/nullCount mismatch: rebuild bit-map or zero nullCount */ }

Prevention

When it happens

Trigger: Constructing PrimitiveColumnContainer with a buffer plus nullBitMap where nullBitMap.IsEmpty but nullCount > 0 — e.g. passing Memory<byte>.Empty for the bit-map while still reporting nulls.

Common situations: Low-level interop or zero-copy code hand-building column buffers; copying column internals and losing the bit-map buffer; optimizations that skip the bit-map but forget to reset nullCount.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/9df60bde95a37d7d. Report an issue: GitHub.