java-native-access/jna · error · NullPointerException

Function address may not be null

Error message

Function address may not be null

What it means

The Pointer-based Function constructor requires a non-null Pointer whose native address (peer) is non-zero. A null pointer or a NULL (0) address cannot be invoked, so JNA throws NullPointerException eagerly rather than crashing inside the native call.

Source

Thrown at src/com/sun/jna/Function.java:278

     * Create a new <code>Function</code> that is linked with a native
     * function that follows the given calling convention.
     *
     * <p>The allocated instance represents a pointer to the given
     * function address, called with the given calling
     * convention.
     *
     * @param  functionAddress
     *                 Address of the native function
     * @param  callFlags
     *                 Function <a href="#callflags">call flags</a>
     * @param  encoding
     *                 Encoding for conversion between Java and native strings.
     */
    Function(Pointer functionAddress, int callFlags, String encoding) {
        checkCallingConvention(callFlags & MASK_CC);
        if (functionAddress == null
            || functionAddress.peer == 0) {
            throw new NullPointerException("Function address may not be null");
        }
        this.functionName = functionAddress.toString();
        this.callFlags = callFlags;
        this.peer = functionAddress.peer;
        this.options = Collections.EMPTY_MAP;
        this.encoding = encoding != null
            ? encoding : Native.getDefaultStringEncoding();
    }

    private void checkCallingConvention(int convention)
        throws IllegalArgumentException {
        // TODO: perform per-platform calling convention checks
        if ((convention & MASK_CC) != convention) {
            throw new IllegalArgumentException("Unrecognized calling convention: "
                                               + convention);
        }
    }

View on GitHub (pinned to d036ad9781)

Solutions

  1. Check the Pointer for null and use Native.NULL-style zero checks (pointer.peer != 0) before constructing a Function
  2. Handle the null return from the native call that produced the pointer before using it
  3. If a function may legitimately be absent, resolve it via library.getSymbolAddress in a try/catch rather than building a Function from a null address

Example fix

// before
Function f = new Function(ptr, Function.C_CONVENTION, "UTF-8");
// after
if (ptr != null && ptr.peer != 0) {
    Function f = new Function(ptr, Function.C_CONVENTION, "UTF-8");
}
Defensive patterns

Strategy: validation

Validate before calling

if (ptr == null || ptr.peer == 0) throw new IllegalStateException("Cannot create Function from NULL address");

Type guard

boolean isCallable(Pointer p) { return p != null && p.peer != 0; }

Try / catch

try {
    Function f = new Function(ptr, Function.C_CONVENTION, null);
} catch (NullPointerException e) {
    // treat as absent/unresolvable function
}

Prevention

When it happens

Trigger: new Function(pointer, callFlags, encoding) where pointer is null, or where the Pointer wraps address 0 (e.g. obtained from a failed native call returning NULL).

Common situations: Storing the result of a native function that returned NULL and later treating it as callable; forgetting to check the result of getPointer()/getSymbolAddress before constructing a Function.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/235e86f4c66ea0ec. Report an issue: GitHub.