oracle/graal · error · NullPointerException

For instance methods, the receiver argument must not be null

Error message

For instance methods, the receiver argument must not be null

What it means

Thrown (as NullPointerException) by HostVMAccess.invoke when an instance method is invoked with a null receiver reference. The host VM needs an actual object to dispatch on, and unlike the null-constant case this one is a programming error (null literal passed) rather than a bad constant value.

Source

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

    @Override
    public boolean owns(ResolvedJavaField value) {
        return value.getClass().getModule() == hostImplModule;
    }

    @Override
    public JavaConstant invoke(ResolvedJavaMethod method, JavaConstant receiver, JavaConstant... arguments) {
        SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();
        Executable executable = snippetReflection.originalMethod(method);
        makeAccessible(executable);
        boolean isConstructor = executable instanceof Constructor;
        Class<?>[] parameterTypes = executable.getParameterTypes();
        if (Modifier.isStatic(executable.getModifiers()) || isConstructor) {
            if (receiver != null) {
                throw new IllegalArgumentException("For static methods or constructor, the receiver argument must be null");
            }
        } else if (receiver == null) {
            throw new NullPointerException("For instance methods, the receiver argument must not be null");
        } else if (receiver.isNull()) {
            throw new IllegalArgumentException("For instance methods, the receiver argument must not represent a null constant");
        }
        if (parameterTypes.length != arguments.length) {
            throw new IllegalArgumentException("Wrong number of arguments: expected " + parameterTypes.length + " but got " + arguments.length);
        }
        Signature signature = method.getSignature();
        Object[] unboxedArguments = new Object[parameterTypes.length];
        for (int i = 0; i < unboxedArguments.length; i++) {
            JavaKind parameterKind = signature.getParameterKind(i);
            JavaConstant argument = arguments[i];
            if (parameterKind.isObject()) {
                if (argument.isNull()) {
                    unboxedArguments[i] = null;
                } else {
                    unboxedArguments[i] = snippetReflection.asObject(parameterTypes[i], argument);
                    if (unboxedArguments[i] == null) {
                        throw new IllegalArgumentException(

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Construct and pass a real receiver constant: snippetReflection.forObject(targetObject)
  2. Ensure the method you resolved is the one you think — if it should be static, re-check the original method
  3. Default the receiver to forObject(...) rather than null in shared call paths

Example fix

// before
JavaConstant receiver = useReceiver ? snippetReflection.forObject(obj) : null;
hostVM.invoke(instanceMethod, receiver, args);  // throws NPE when useReceiver==false

// after
hostVM.invoke(instanceMethod, snippetReflection.forObject(obj), args);
Defensive patterns

Strategy: validation

Validate before calling

if (!Modifier.isStatic(executable.getModifiers()) && receiver == null) {
    throw new NullPointerException("receiver required for " + executable);

Try / catch

catch (NullPointerException e) { surface which method lacked a receiver; fix the call site }

Prevention

When it happens

Trigger: Calling invoke(instanceMethod, null, args...) where instanceMethod maps to a non-static, non-constructor Executable. The preceding branch already ensures static/constructor calls got null legally; reaching this throw means an instance method got a literal null receiver.

Common situations: A caller-side conditional that defaults the receiver to null (e.g. receiver = cond ? forObject(x) : null) then invokes an instance method on the null path; refactoring a static call site into an instance one without updating receiver construction.

Related errors


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