java-native-access/jna · error · java.lang.IllegalArgumentException

null passed to getNativeLibrary

Error message

null passed to getNativeLibrary

What it means

Native.getNativeLibrary(Library) throws this IllegalArgumentException when called with null. The method unwraps the proxy behind a loaded Library interface instance and refuses null input up front. It is a simple argument validation guard.

Source

Thrown at src/com/sun/jna/Native.java:2038

            }
        }
        synchronized(registeredClasses) {
            registeredClasses.put(cls, handles);
            registeredLibraries.put(cls, lib);
        }
    }

    /**
     * Get the {@link NativeLibrary} instance that is wrapped by the given
     * {@link Library} interface instance.
     *
     * @param library the {@link Library} interface instance, which was created
     * by the {@link Native#load Native.load()} method
     * @return the wrapped {@link NativeLibrary} instance
     */
    public static NativeLibrary getNativeLibrary(final Library library) {
        if(library == null) {
            throw new IllegalArgumentException("null passed to getNativeLibrary");
        }
        if(! Proxy.isProxyClass(library.getClass())) {
            throw new IllegalArgumentException("library object passed to getNativeLibrary in not a proxy");
        }
        final InvocationHandler handler = Proxy.getInvocationHandler(library);
        if (!(handler instanceof Library.Handler)) {
            throw new IllegalArgumentException("Object is not a properly initialized Library interface instance");
        }
        return ((Library.Handler) handler).getNativeLibrary();
    }

    /**
     * Get the {@link NativeLibrary} instance to which the given "registered"
     * class is bound.
     *
     * @param cls the "registered" class, which was previously registered via
     * the {@link Native#register register()} method
     * @return the {@link NativeLibrary} instance to which the "registered"

View on GitHub (pinned to d036ad9781)

Solutions

  1. Ensure Native.load()/loadLibrary() was actually called and its non-null result is what you pass.
  2. Null-check or assert the Library reference before calling getNativeLibrary.
  3. Fix initialization order so the library is loaded before this accessor runs.

Example fix

// before
NativeLibrary nl = Native.getNativeLibrary(lib); // lib may be null
// after
Objects.requireNonNull(lib, "library not loaded");
NativeLibrary nl = Native.getNativeLibrary(lib);
Defensive patterns

Strategy: type-guard

Validate before calling

if (lib == null) {
    throw new IllegalStateException("Library not loaded; call Native.load() first");
}

Type guard

boolean isLoadedLibrary(Library lib) { return lib != null; }

Try / catch

try {
    NativeLibrary nl = Native.getNativeLibrary(lib);
} catch (IllegalArgumentException e) {
    if ("null passed to getNativeLibrary".equals(e.getMessage())) {
        throw new IllegalStateException("Library was never loaded", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling Native.getNativeLibrary((Library) null), often when the Library variable was never assigned because load() failed silently or was skipped.

Common situations: Null returned from a factory/dependency-injection container and passed straight through; refactoring where the load call was removed but the accessor remained.

Related errors


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