java-native-access/jna · error · NullPointerException

Function name must not be null

Error message

Function name must not be null

What it means

NullPointerException thrown by the Function constructor when functionName is null. Function objects are created by NativeLibrary when resolving a named symbol, and a null name can never match a native symbol, so JNA fails fast with an explicit message instead of a confusing downstream native lookup failure.

Source

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

     * <p>The allocated instance represents a pointer to the named native
     * function from the supplied library, called with the given calling
     * convention.
     *
     * @param  library
     *                 {@link NativeLibrary} in which to find the function
     * @param  functionName
     *                 Name of the native function to be linked with
     * @param  callFlags
     *                 Function <a href="#callflags">call flags</a>
     * @param  encoding
     *                 Encoding for conversion between Java and native strings.
     * @throws UnsatisfiedLinkError if the given function name is
     * not found within the library.
     */
    Function(NativeLibrary library, String functionName, int callFlags, String encoding) {
        checkCallingConvention(callFlags & MASK_CC);
        if (functionName == null) {
            throw new NullPointerException("Function name must not be null");
        }
        this.library = library;
        this.functionName = functionName;
        this.callFlags = callFlags;
        this.options = library.getOptions();
        this.encoding = encoding != null ? encoding : Native.getDefaultStringEncoding();
        try {
            this.peer = library.getSymbolAddress(functionName);
        } catch(UnsatisfiedLinkError e) {
            throw new UnsatisfiedLinkError("Error looking up function '"
                                           + functionName + "': "
                                           + e.getMessage());
        }
    }

    /**
     * Create a new <code>Function</code> that is linked with a native
     * function that follows the given calling convention.

View on GitHub (pinned to d036ad9781)

Solutions

  1. Ensure the name passed to NativeLibrary.getFunction / the Library method invocation is a non-null String; add a null-check at the call site.
  2. If the name comes from configuration/annotations, validate it during load (fail at startup with a clear message).
  3. If exporting under a different symbol, use the correct mapped name (e.g. @FunctionName annotation value or the actual C symbol from 'nm' / 'dumpbin /exports').

Example fix

// before
String sym = props.getProperty("exported"); // null
Function f = lib.getFunction(sym); // NullPointerException
// after
String sym = props.getProperty("exported");
Objects.requireNonNull(sym, "exported symbol name must be configured");
Function f = lib.getFunction(sym);
Defensive patterns

Strategy: validation

Validate before calling

Function getFunctionChecked(NativeLibrary lib, String name) {
    java.util.Objects.requireNonNull(name, "function name must not be null");
    if (name.isEmpty()) throw new IllegalArgumentException("function name must not be empty");
    return lib.getFunction(name);
}

Type guard

static boolean hasFunctionName(String name) {
    return name != null && !name.isEmpty();
}

Try / catch

try { f = lib.getFunction(name); } catch (NullPointerException e) { if ("Function name must not be null".equals(e.getMessage())) { /* fix name resolution/config */ } throw e; }

Prevention

When it happens

Trigger: Requesting a function with a null name: nativeLibrary.getFunction(null), Library interface proxies whose method name resolves to null (rare), custom code building Function instances directly with null, or config-driven mappings where the symbol name field was never populated.

Common situations: Configuration files or annotation processors that leave the exported symbol name empty; reflection-driven wrappers passing Method.getName() of a synthesized method; calling NativeLibrary.getFunction with a variable that was expected to be initialized.

Related errors


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