{"record":{"id":"235e86f4c66ea0ec","repo":"java-native-access/jna","slug":"function-address-may-not-be-null","errorCode":null,"errorMessage":"Function address may not be null","messagePattern":"Function address may not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Function.java","lineNumber":278,"sourceCode":"     * Create a new <code>Function</code> that is linked with a native\n     * function that follows the given calling convention.\n     *\n     * <p>The allocated instance represents a pointer to the given\n     * function address, called with the given calling\n     * convention.\n     *\n     * @param  functionAddress\n     *                 Address of the native function\n     * @param  callFlags\n     *                 Function <a href=\"#callflags\">call flags</a>\n     * @param  encoding\n     *                 Encoding for conversion between Java and native strings.\n     */\n    Function(Pointer functionAddress, int callFlags, String encoding) {\n        checkCallingConvention(callFlags & MASK_CC);\n        if (functionAddress == null\n            || functionAddress.peer == 0) {\n            throw new NullPointerException(\"Function address may not be null\");\n        }\n        this.functionName = functionAddress.toString();\n        this.callFlags = callFlags;\n        this.peer = functionAddress.peer;\n        this.options = Collections.EMPTY_MAP;\n        this.encoding = encoding != null\n            ? encoding : Native.getDefaultStringEncoding();\n    }\n\n    private void checkCallingConvention(int convention)\n        throws IllegalArgumentException {\n        // TODO: perform per-platform calling convention checks\n        if ((convention & MASK_CC) != convention) {\n            throw new IllegalArgumentException(\"Unrecognized calling convention: \"\n                                               + convention);\n        }\n    }\n","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Function.java#L260-L296","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the Pointer for null and use Native.NULL-style zero checks (pointer.peer != 0) before constructing a Function","Handle the null return from the native call that produced the pointer before using it","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"],"exampleFix":"// before\nFunction f = new Function(ptr, Function.C_CONVENTION, \"UTF-8\");\n// after\nif (ptr != null && ptr.peer != 0) {\n    Function f = new Function(ptr, Function.C_CONVENTION, \"UTF-8\");\n}","handlingStrategy":"validation","validationCode":"if (ptr == null || ptr.peer == 0) throw new IllegalStateException(\"Cannot create Function from NULL address\");","typeGuard":"boolean isCallable(Pointer p) { return p != null && p.peer != 0; }","tryCatchPattern":"try {\n    Function f = new Function(ptr, Function.C_CONVENTION, null);\n} catch (NullPointerException e) {\n    // treat as absent/unresolvable function\n}","preventionTips":["Always null-check and zero-address-check Pointers received from native calls before wrapping them in a Function","Treat a NULL native return as a distinct code path, never as a callable function"],"tags":["jna","null-pointer","native"],"backgroundTag":"null-argument","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}