{"record":{"id":"b835b586bed5777d","repo":"redis/jedis","slug":"a-null-argument-cannot-be-sent-to-redis","errorCode":null,"errorMessage":"A null argument cannot be sent to Redis.","messagePattern":"A null argument cannot be sent to Redis\\.","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/search/RediSearchUtil.java","lineNumber":41,"sourceCode":"  public static Map<String, String> toStringMap(Map<String, Object> input) {\n    return toStringMap(input, false);\n  }\n\n  /**\n   * Jedis' {@code hset} methods do not support {@link Object}s as values. This method eases process\n   * of converting a {@link Map} with Objects as values so that the returning Map can be set to a\n   * {@code hset} method.\n   * @param input map with object value\n   * @param stringEscape whether to escape the String objects\n   * @return map with string value\n   */\n  public static Map<String, String> toStringMap(Map<String, Object> input, boolean stringEscape) {\n    Map<String, String> output = new HashMap<>(input.size());\n    for (Map.Entry<String, Object> entry : input.entrySet()) {\n      String key = entry.getKey();\n      Object obj = entry.getValue();\n      if (key == null || obj == null) {\n        throw new NullPointerException(\"A null argument cannot be sent to Redis.\");\n      }\n      String str;\n      if (obj instanceof byte[]) {\n        str = SafeEncoder.encode((byte[]) obj);\n      } else if (obj instanceof redis.clients.jedis.GeoCoordinate) {\n        redis.clients.jedis.GeoCoordinate geo = (redis.clients.jedis.GeoCoordinate) obj;\n        str = geo.getLongitude() + \",\" + geo.getLatitude();\n      } else if (obj instanceof String) {\n        str = stringEscape ? escape((String) obj) : (String) obj;\n      } else {\n        str = String.valueOf(obj);\n      }\n      output.put(key, str);\n    }\n    return output;\n  }\n\n  /**","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/search/RediSearchUtil.java#L23-L59","documentation":"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.","triggerScenarios":"Calling toStringMap with a map containing null keys or null values, e.g. attributes built from incomplete user input or Optional.orElse(null) patterns.","commonSituations":"Document/attribute maps assembled from JSON deserialization where fields are missing; search filter params with unset values; dynamic metadata maps with absent keys.","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"],"exampleFix":"// before\nMap<String,String> attrs = RediSearchUtil.toStringMap(input, true);\n// after\ninput.values().removeIf(Objects::isNull);\nMap<String,String> attrs = RediSearchUtil.toStringMap(input, true);","handlingStrategy":"validation","validationCode":"Map<String,Object> safe = new HashMap<>();\ninput.forEach((k, v) -> { if (k != null && v != null) safe.put(k, v); });\nMap<String,String> out = RediSearchUtil.toStringMap(safe, stringEscape);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["redisearch","search","null-pointer","map"],"backgroundTag":"null-argument","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}