elastic/elasticsearch · error · IllegalArgumentException

input cannot be null

Error message

input cannot be null

What it means

Thrown by DatasetUtilsImpl.fromInput when the input MemorySegment is null. fromInput wraps a native memory segment as a CuVSMatrix; a null segment has no backing memory to read vectors from, so it is rejected before any dimension or size check.

Source

Thrown at libs/gpu-codec/src/main/java/org/elasticsearch/gpu/codec/DatasetUtilsImpl.java:47

        try {
            return (CuVSMatrix) createDataset$mh.invokeExact(memorySegment, size, dimensions, dataType);
        } catch (Throwable e) {
            if (e instanceof Error err) {
                throw err;
            } else if (e instanceof RuntimeException re) {
                throw re;
            } else {
                throw new RuntimeException(e);
            }
        }
    }

    private DatasetUtilsImpl() {}

    @Override
    public CuVSMatrix fromInput(MemorySegment input, int numVectors, int dims, CuVSMatrix.DataType dataType) {
        if (input == null) {
            throw new IllegalArgumentException("input cannot be null");
        }
        if (numVectors < 0 || dims < 0) {
            throwIllegalArgumentException(numVectors, dims);
        }
        final int byteSize = dataType == CuVSMatrix.DataType.FLOAT ? Float.BYTES : Byte.BYTES;
        if (((long) numVectors * dims * byteSize) > input.byteSize()) {
            throwIllegalArgumentException(input, numVectors, dims);
        }
        return fromMemorySegment(input, numVectors, dims, dataType);
    }

    static void throwIllegalArgumentException(MemorySegment ms, int numVectors, int dims) {
        var s = "segment of size [" + ms.byteSize() + "] too small for expected " + numVectors + " float vectors of " + dims + " dims";
        throw new IllegalArgumentException(s);
    }

    static void throwIllegalArgumentException(int numVectors, int dims) {
        String s;

View on GitHub (pinned to db6a809a66)

Solutions

  1. Ensure the MemorySegment is allocated before calling fromInput; check for null after allocation and handle the failure.
  2. If allocation can fail (native OOM), propagate a meaningful error rather than passing null downstream.
  3. In tests, allocate a real MemorySegment of sufficient size (numVectors * dims * elementByteSize).

Example fix

// before: passing a possibly-null segment
MemorySegment seg = maybeAllocate(numVectors * dims * Float.BYTES);
CuVSMatrix m = datasetUtils.fromInput(seg, numVectors, dims, DataType.FLOAT);

// after: null-check after allocation
MemorySegment seg = maybeAllocate(numVectors * dims * Float.BYTES);
if (seg == null) {
    throw new OutOfMemoryError("failed to allocate GPU dataset segment");
}
CuVSMatrix m = datasetUtils.fromInput(seg, numVectors, dims, DataType.FLOAT);
Defensive patterns

Strategy: validation

Validate before calling

static CuVSMatrix safeFromInput(MemorySegment input, int numVectors, int dims, CuVSMatrix.DataType dt) {
    if (input == null) {
        throw new OutOfMemoryError("failed to allocate dataset segment (null)");
    }
    return datasetUtils.fromInput(input, numVectors, dims, dt);
}

Type guard

static boolean isAllocated(MemorySegment seg) {
    return seg != null && seg.byteSize() > 0;
}

Try / catch

try {
    return datasetUtils.fromInput(input, numVectors, dims, dt);
} catch (IllegalArgumentException e) {
    if (e.getMessage().equals("input cannot be null")) {
        // allocation failed upstream; rethrow as OOM with context
        throw new OutOfMemoryError("GPU dataset segment was null");
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling datasetUtils.fromInput(null, numVectors, dims, dataType) — the first guard in fromInput checks input == null and throws. Reachable when a caller passes a MemorySegment that was never allocated or whose allocation failed and returned null.

Common situations: A memory allocation that returned null on failure (out of native memory) being passed through unchecked; a code path that conditionally allocates the segment and passes it even when allocation was skipped; a test passing null inadvertently.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/3e80602c87f876ca. Report an issue: GitHub.