java-native-access/jna · error · java.lang.NullPointerException
String initializer must be non-null
Error message
String initializer must be non-null
What it means
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.
Source
Thrown at src/com/sun/jna/WString.java:33
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna;
/** Simple wrapper class to identify a wide string argument or return type.
* @author twall@users.sf.net
*/
public final class WString implements CharSequence, Comparable {
private String string;
public WString(String s){
if (s == null) {
throw new NullPointerException("String initializer must be non-null");
}
this.string = s;
}
@Override
public String toString() {
return string;
}
@Override
public boolean equals(Object o) {
return (o instanceof WString) && toString().equals(o.toString());
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public int compareTo(Object o) {
return toString().compareTo(o.toString());View on GitHub (pinned to d036ad9781)
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.
Example fix
// before
new WString(maybeNull);
// after
if (maybeNull != null) {
new WString(maybeNull);
} else {
Pointer.NULL; // or handle absence explicitly
} Defensive patterns
Strategy: validation
Validate before calling
// Java
if (s == null) throw new IllegalArgumentException("wide string required");
WString ws = new WString(s); Type guard
WString safeWString(String s) { return (s == null) ? null : new WString(s); } Try / catch
try { return new WString(s); } catch (NullPointerException e) { return null; /* or Pointer.NULL semantics */ } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Function address may not be null
- String must not be null
- No support for " + os
- Set sun.java2d.noddraw=true to enable transparent windows
- Window must be a RootPaneContainer
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/678617db3795347f.
Report an issue: GitHub.