redis/jedis · error · NullPointerException
A null argument cannot be sent to Redis.
Error message
A null argument cannot be sent to Redis.
What it means
RediSearchUtil.toStringMap converts a Map<String,Object> into a flat String map for RediSearch commands. A NullPointerException is thrown when any key or value in the input map is null, since the Redis protocol cannot transmit null arguments.
Solutions
- Sanitize the map before the call: remove entries with null keys or values
- Substitute sensible defaults for null values (e.g. empty string) if the semantics allow
- If a null signals 'not present', omit the attribute rather than sending it
Example fix
// before Map<String,String> attrs = RediSearchUtil.toStringMap(input, true); // after input.values().removeIf(Objects::isNull); Map<String,String> attrs = RediSearchUtil.toStringMap(input, true);
Defensive patterns
Strategy: validation
Validate before calling
Map<String,Object> safe = new HashMap<>();
input.forEach((k, v) -> { if (k != null && v != null) safe.put(k, v); });
Map<String,String> out = RediSearchUtil.toStringMap(safe, stringEscape); Prevention
- Strip null keys/values from attribute maps before sending to RediSearch
- Avoid orElse(null) when building search attribute maps
- Validate deserialized documents for missing fields before indexing
When it happens
Trigger: Calling toStringMap with a map containing null keys or null values, e.g. attributes built from incomplete user input or Optional.orElse(null) patterns.
Common situations: Document/attribute maps assembled from JSON deserialization where fields are missing; search filter params with unset values; dynamic metadata maps with absent keys.
Related errors
- Setting null as field attribute is not allowed.
- Attribute for this field is already set.
- Unrecognized header
- DIALECT=0 cannot be set.
- DIALECT=0 cannot be set.
AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08).
Data as JSON: /api/errors/b835b586bed5777d.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/redis/clients/jedis/search/RediSearchUtil.java:41
public static Map<String, String> toStringMap(Map<String, Object> input) {
return toStringMap(input, false);
}
/**
* Jedis' {@code hset} methods do not support {@link Object}s as values. This method eases process
* of converting a {@link Map} with Objects as values so that the returning Map can be set to a
* {@code hset} method.
* @param input map with object value
* @param stringEscape whether to escape the String objects
* @return map with string value
*/
public static Map<String, String> toStringMap(Map<String, Object> input, boolean stringEscape) {
Map<String, String> output = new HashMap<>(input.size());
for (Map.Entry<String, Object> entry : input.entrySet()) {
String key = entry.getKey();
Object obj = entry.getValue();
if (key == null || obj == null) {
throw new NullPointerException("A null argument cannot be sent to Redis.");
}
String str;
if (obj instanceof byte[]) {
str = SafeEncoder.encode((byte[]) obj);
} else if (obj instanceof redis.clients.jedis.GeoCoordinate) {
redis.clients.jedis.GeoCoordinate geo = (redis.clients.jedis.GeoCoordinate) obj;
str = geo.getLongitude() + "," + geo.getLatitude();
} else if (obj instanceof String) {
str = stringEscape ? escape((String) obj) : (String) obj;
} else {
str = String.valueOf(obj);
}
output.put(key, str);
}
return output;
}
/**View on GitHub (pinned to 6dac31d4c2)