{"record":{"id":"678617db3795347f","repo":"java-native-access/jna","slug":"string-initializer-must-be-non-null","errorCode":null,"errorMessage":"String initializer must be non-null","messagePattern":"String initializer must be non-null","errorType":"exception","errorClass":"java.lang.NullPointerException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/WString.java","lineNumber":33,"sourceCode":" * containing JNA, in file \"LGPL2.1\".\n *\n * You may obtain a copy of the Apache License at:\n *\n * http://www.apache.org/licenses/\n *\n * A copy is also included in the downloadable source code package\n * containing JNA, in file \"AL2.0\".\n */\npackage com.sun.jna;\n\n/** Simple wrapper class to identify a wide string argument or return type.\n * @author twall@users.sf.net\n */\npublic final class WString implements CharSequence, Comparable {\n    private String string;\n    public WString(String s){\n        if (s == null) {\n            throw new NullPointerException(\"String initializer must be non-null\");\n        }\n        this.string = s;\n    }\n    @Override\n    public String toString() {\n        return string;\n    }\n    @Override\n    public boolean equals(Object o) {\n        return (o instanceof WString) && toString().equals(o.toString());\n    }\n    @Override\n    public int hashCode() {\n        return toString().hashCode();\n    }\n    @Override\n    public int compareTo(Object o) {\n        return toString().compareTo(o.toString());","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/WString.java#L15-L51","documentation":"The WString constructor throws NullPointerException when given a null string. WString wraps a Java String as a wide (wchar_t*) native string, so a null initializer has no meaningful representation and JNA fails fast at construction.","triggerScenarios":"Calling new WString(null) directly, or passing a null String through an API that converts to WString (e.g. function arguments or Native.toString-style helpers).","commonSituations":"Null string variables coming from configuration or native calls being wrapped as WString; missing null checks before marshalling arguments to a wide-string native function.","solutions":["Check the string for null before constructing WString and substitute an empty string or skip the call.","If null must map to a native NULL, pass a typed null pointer (Pointer.NULL / null Pointer argument) instead of WString.","Fix the upstream source returning the null string."],"exampleFix":"// before\nnew WString(maybeNull);\n// after\nif (maybeNull != null) {\n    new WString(maybeNull);\n} else {\n    Pointer.NULL; // or handle absence explicitly\n}","handlingStrategy":"validation","validationCode":"// Java\nif (s == null) throw new IllegalArgumentException(\"wide string required\");\nWString ws = new WString(s);","typeGuard":"WString safeWString(String s) { return (s == null) ? null : new WString(s); }","tryCatchPattern":"try { return new WString(s); } catch (NullPointerException e) { return null; /* or Pointer.NULL semantics */ }","preventionTips":["Null-check strings before wrapping as WString","Model native NULL with Pointer.NULL rather than a null-backed WString","Validate configuration/string sources before marshalling"],"tags":["jna","wstring","null-pointer","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"}