{"record":{"id":"eba5de2aa33d83a2","repo":"java-native-access/jna","slug":"function-name-must-not-be-null","errorCode":null,"errorMessage":"Function name must not be null","messagePattern":"Function name must not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Function.java","lineNumber":243,"sourceCode":"     * <p>The allocated instance represents a pointer to the named native\n     * function from the supplied library, called with the given calling\n     * convention.\n     *\n     * @param  library\n     *                 {@link NativeLibrary} in which to find the function\n     * @param  functionName\n     *                 Name of the native function to be linked with\n     * @param  callFlags\n     *                 Function <a href=\"#callflags\">call flags</a>\n     * @param  encoding\n     *                 Encoding for conversion between Java and native strings.\n     * @throws UnsatisfiedLinkError if the given function name is\n     * not found within the library.\n     */\n    Function(NativeLibrary library, String functionName, int callFlags, String encoding) {\n        checkCallingConvention(callFlags & MASK_CC);\n        if (functionName == null) {\n            throw new NullPointerException(\"Function name must not be null\");\n        }\n        this.library = library;\n        this.functionName = functionName;\n        this.callFlags = callFlags;\n        this.options = library.getOptions();\n        this.encoding = encoding != null ? encoding : Native.getDefaultStringEncoding();\n        try {\n            this.peer = library.getSymbolAddress(functionName);\n        } catch(UnsatisfiedLinkError e) {\n            throw new UnsatisfiedLinkError(\"Error looking up function '\"\n                                           + functionName + \"': \"\n                                           + e.getMessage());\n        }\n    }\n\n    /**\n     * Create a new <code>Function</code> that is linked with a native\n     * function that follows the given calling convention.","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Function.java#L225-L261","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the name passed to NativeLibrary.getFunction / the Library method invocation is a non-null String; add a null-check at the call site.","If the name comes from configuration/annotations, validate it during load (fail at startup with a clear message).","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')."],"exampleFix":"// before\nString sym = props.getProperty(\"exported\"); // null\nFunction f = lib.getFunction(sym); // NullPointerException\n// after\nString sym = props.getProperty(\"exported\");\nObjects.requireNonNull(sym, \"exported symbol name must be configured\");\nFunction f = lib.getFunction(sym);","handlingStrategy":"validation","validationCode":"Function getFunctionChecked(NativeLibrary lib, String name) {\n    java.util.Objects.requireNonNull(name, \"function name must not be null\");\n    if (name.isEmpty()) throw new IllegalArgumentException(\"function name must not be empty\");\n    return lib.getFunction(name);\n}","typeGuard":"static boolean hasFunctionName(String name) {\n    return name != null && !name.isEmpty();\n}","tryCatchPattern":"try { f = lib.getFunction(name); } catch (NullPointerException e) { if (\"Function name must not be null\".equals(e.getMessage())) { /* fix name resolution/config */ } throw e; }","preventionTips":["Validate symbol names from config/annotations at startup.","Never build Function objects from unvalidated dynamic names.","Verify exported symbol names with nm/dumpbin when binding manually.","Prefer explicit String literals or annotated names over reflection-derived ones."],"tags":["jna","null-argument","function-lookup"],"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"}