{"record":{"id":"d91108de1b75a5f3","repo":"java-native-access/jna","slug":"null-passed-to-getnativelibrary","errorCode":null,"errorMessage":"null passed to getNativeLibrary","messagePattern":"null passed to getNativeLibrary","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":2038,"sourceCode":"            }\n        }\n        synchronized(registeredClasses) {\n            registeredClasses.put(cls, handles);\n            registeredLibraries.put(cls, lib);\n        }\n    }\n\n    /**\n     * Get the {@link NativeLibrary} instance that is wrapped by the given\n     * {@link Library} interface instance.\n     *\n     * @param library the {@link Library} interface instance, which was created\n     * by the {@link Native#load Native.load()} method\n     * @return the wrapped {@link NativeLibrary} instance\n     */\n    public static NativeLibrary getNativeLibrary(final Library library) {\n        if(library == null) {\n            throw new IllegalArgumentException(\"null passed to getNativeLibrary\");\n        }\n        if(! Proxy.isProxyClass(library.getClass())) {\n            throw new IllegalArgumentException(\"library object passed to getNativeLibrary in not a proxy\");\n        }\n        final InvocationHandler handler = Proxy.getInvocationHandler(library);\n        if (!(handler instanceof Library.Handler)) {\n            throw new IllegalArgumentException(\"Object is not a properly initialized Library interface instance\");\n        }\n        return ((Library.Handler) handler).getNativeLibrary();\n    }\n\n    /**\n     * Get the {@link NativeLibrary} instance to which the given \"registered\"\n     * class is bound.\n     *\n     * @param cls the \"registered\" class, which was previously registered via\n     * the {@link Native#register register()} method\n     * @return the {@link NativeLibrary} instance to which the \"registered\"","sourceCodeStart":2020,"sourceCodeEnd":2056,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L2020-L2056","documentation":"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.","triggerScenarios":"Calling Native.getNativeLibrary((Library) null), often when the Library variable was never assigned because load() failed silently or was skipped.","commonSituations":"Null returned from a factory/dependency-injection container and passed straight through; refactoring where the load call was removed but the accessor remained.","solutions":["Ensure Native.load()/loadLibrary() was actually called and its non-null result is what you pass.","Null-check or assert the Library reference before calling getNativeLibrary.","Fix initialization order so the library is loaded before this accessor runs."],"exampleFix":"// before\nNativeLibrary nl = Native.getNativeLibrary(lib); // lib may be null\n// after\nObjects.requireNonNull(lib, \"library not loaded\");\nNativeLibrary nl = Native.getNativeLibrary(lib);","handlingStrategy":"type-guard","validationCode":"if (lib == null) {\n    throw new IllegalStateException(\"Library not loaded; call Native.load() first\");\n}","typeGuard":"boolean isLoadedLibrary(Library lib) { return lib != null; }","tryCatchPattern":"try {\n    NativeLibrary nl = Native.getNativeLibrary(lib);\n} catch (IllegalArgumentException e) {\n    if (\"null passed to getNativeLibrary\".equals(e.getMessage())) {\n        throw new IllegalStateException(\"Library was never loaded\", e);\n    }\n    throw e;\n}","preventionTips":["Store the loaded Library in a final field initialized at startup.","Use Objects.requireNonNull on load results.","Initialize the library once in a static holder before any accessor runs."],"tags":["jna","null-argument","illegal-argument"],"backgroundTag":"null-argument","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}