oracle/graal · error · IllegalArgumentException

For instance fields, the receiver argument must not represen

Error message

For instance fields, the receiver argument must not represent a null constant

What it means

Thrown by HostVMAccess.writeField when writing an instance field with a receiver constant that represents null (receiver.isNull()). Reflection cannot store a field on a null object, so the null constant is rejected explicitly even though the reference itself is non-null.

Source

Thrown at compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java:232

            throw new RuntimeException("Should not reach here", e);
        }
    }

    @Override
    public void writeField(ResolvedJavaField field, JavaConstant receiver, JavaConstant value) {
        SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();
        Field reflectionField = snippetReflection.originalField(field);
        makeAccessible(reflectionField);
        var fieldKind = field.getJavaKind();

        if (Modifier.isStatic(reflectionField.getModifiers())) {
            if (receiver != null) {
                throw new IllegalArgumentException("For static fields, the receiver argument must be null");
            }
        } else if (receiver == null) {
            throw new NullPointerException("For instance fields, the receiver argument must not be null");
        } else if (receiver.isNull()) {
            throw new IllegalArgumentException("For instance fields, the receiver argument must not represent a null constant");
        }

        Object unboxedValue;
        if (fieldKind.isObject()) {
            unboxedValue = snippetReflection.asObject(reflectionField.getType(), value);
        } else {
            assert fieldKind.isPrimitive();
            if (fieldKind != value.getJavaKind()) {
                throw new IllegalArgumentException("Expected value kind " + fieldKind + " but got " + value.getJavaKind());
            }
            unboxedValue = value.asBoxedPrimitive();
        }

        Object unboxedReceiver;
        if (Modifier.isStatic(reflectionField.getModifiers())) {
            unboxedReceiver = null;
        } else {
            unboxedReceiver = snippetReflection.asObject(reflectionField.getDeclaringClass(), receiver);

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Guard before the call: if the receiver constant isNull(), raise the guest NullPointerException explicitly or skip the store
  2. Only call forObject on non-null holders; keep null as a literal null reference reserved for static fields
  3. Validate holder objects at the source (lookup/creation) so null never reaches writeField

Example fix

// before
hostVM.writeField(instanceField, snippetReflection.forObject(maybeNullHolder), value);  // throws

// after
if (maybeNullHolder == null) {
    throw new NullPointerException("holder"); // explicit semantics
}
hostVM.writeField(instanceField, snippetReflection.forObject(maybeNullHolder), value);
Defensive patterns

Strategy: type-guard

Validate before calling

if (receiver != null && receiver.isNull() && !field.isStatic()) {
    throw new NullPointerException("null holder for instance field " + field);

Type guard

static boolean isNullConstant(JavaConstant c) { return c != null && c.isNull(); }

Try / catch

catch (IllegalArgumentException e) { translate to guest NPE semantics }

Prevention

When it happens

Trigger: Calling writeField(instanceField, JavaConstant.NULL_POINTER, value) or any constant produced by forObject(null), for a non-static field.

Common situations: Uniform wrapping of possibly-null holders via snippetReflection.forObject(holder) which silently yields the null constant; guest-level null receivers reaching host field stores during snippet unfolding.

Related errors


AI-assisted analysis of oracle/graal@a66e9ccd1d (2026-08-14). Data as JSON: /api/errors/1e3e569e47adaedc. Report an issue: GitHub.