{"record":{"id":"073a0ad1d1ec7ede","repo":"java-native-access/jna","slug":"string-must-not-be-null","errorCode":null,"errorMessage":"String must not be null","messagePattern":"String must not be null","errorType":"exception","errorClass":"java.lang.NullPointerException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/NativeString.java","lineNumber":78,"sourceCode":"     * @param wide whether to store the String as <code>wchar_t</code>\n     */\n    public NativeString(String string, boolean wide) {\n        this(string, wide ? WIDE_STRING : Native.getDefaultStringEncoding());\n    }\n\n    /** Create a native string as a NUL-terminated array of\n     * <code>wchar_t</code>.\n     */\n    public NativeString(WString string) {\n        this(string.toString(), WIDE_STRING);\n    }\n\n    /** Create a native string (NUL-terminated array of <code>char</code>),\n     * using the requested encoding.\n     */\n    public NativeString(String string, String encoding) {\n        if (string == null) {\n            throw new NullPointerException(\"String must not be null\");\n        }\n        // Allocate the memory to hold the string.  Note, we have to\n        // make this 1 element longer in order to accommodate the terminating\n        // NUL (which is generated in Pointer.setString()).\n        this.encoding = encoding;\n        if (WIDE_STRING.equals(this.encoding)) {\n            int len = (string.length() + 1 ) * Native.WCHAR_SIZE;\n            pointer = new StringMemory(len);\n            pointer.setWideString(0, string);\n        } else {\n            byte[] data = Native.getBytes(string, encoding);\n            pointer = new StringMemory(data.length + 1);\n            pointer.write(0, data, 0, data.length);\n            pointer.setByte(data.length, (byte)0);\n        }\n    }\n\n    @Override","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/NativeString.java#L60-L96","documentation":"NativeString's constructor throws NullPointerException when the Java String is null. A native string requires actual characters to copy into allocated memory; JNA refuses to silently map null to an empty or zero pointer.","triggerScenarios":"Creating new NativeString(null, encoding) directly, or passing a null String to a mapped function argument where the parameter type is String and no null-substitution is configured.","commonSituations":"Null values coming from parsing/user input passed straight into native calls; API surfaces that allow null Strings reaching a structure field of native-string type.","solutions":["Check the string for null before creating the NativeString or invoking the function","Pass an explicit empty string (\"\") if the native side expects a valid string","Use a mapped null handling (custom TypeMapper that maps null to a NULL Pointer) if null is a valid native value","In Structure fields of type String, ensure setField/constructor values are non-null or use Pointer NULL explicitly"],"exampleFix":"// before\nNativeString ns = new NativeString(maybeNull, \"UTF-8\");\n// after\nNativeString ns = maybeNull != null\n    ? new NativeString(maybeNull, \"UTF-8\")\n    : null; // or new NativeString(\"\", \"UTF-8\")","handlingStrategy":"validation","validationCode":"if (s == null) {\n    s = \"\"; // or reject\n}","typeGuard":"static String requireNonNullString(String s) {\n    if (s == null) throw new IllegalArgumentException(\"string required\");\n    return s;\n}","tryCatchPattern":"try {\n    NativeString ns = new NativeString(s, \"UTF-8\");\n} catch (NullPointerException e) {\n    // s was null; substitute \"\" or a NULL Pointer\n}","preventionTips":["Null-check all strings destined for native calls","Prefer mapping null to Pointer.NULL via a TypeMapper when null is a valid native value"],"tags":["jna","null-argument","native-string"],"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"}