apache/kafka · error · ConfigException
Invalid value null for configuration value.serializer: must
Error message
Invalid value null for configuration value.serializer: must be non-null.
What it means
Thrown by ProducerConfig.appendSerializerToConfig when no value serializer can be determined. Symmetric to the key serializer check: the producer requires a Serializer for every record value, and if neither a valueSerializer instance was passed to the KafkaProducer constructor nor a `value.serializer` class is present in the config map, this ConfigException is raised naming VALUE_SERIALIZER_CLASS_CONFIG with value null.
Source
Thrown at clients/src/main/java/org/apache/kafka/clients/producer/ProducerConfig.java:725
return acksString.trim().equalsIgnoreCase("all") ? "-1" : Short.parseShort(acksString.trim()) + "";
} catch (NumberFormatException e) {
throw new ConfigException("Invalid configuration value for 'acks': " + acksString);
}
}
static Map<String, Object> appendSerializerToConfig(Map<String, Object> configs,
Serializer<?> keySerializer,
Serializer<?> valueSerializer) {
// validate serializer configuration, if the passed serializer instance is null, the user must explicitly set a valid serializer configuration value
Map<String, Object> newConfigs = new HashMap<>(configs);
if (keySerializer != null)
newConfigs.put(KEY_SERIALIZER_CLASS_CONFIG, keySerializer.getClass());
else if (newConfigs.get(KEY_SERIALIZER_CLASS_CONFIG) == null)
throw new ConfigException(KEY_SERIALIZER_CLASS_CONFIG, null, "must be non-null.");
if (valueSerializer != null)
newConfigs.put(VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer.getClass());
else if (newConfigs.get(VALUE_SERIALIZER_CLASS_CONFIG) == null)
throw new ConfigException(VALUE_SERIALIZER_CLASS_CONFIG, null, "must be non-null.");
return newConfigs;
}
/**
* Constructs a new ProducerConfig with the given properties.
*
* @param props The producer configuration properties
*/
public ProducerConfig(Properties props) {
super(CONFIG, props);
}
/**
* Constructs a new ProducerConfig with the given configuration map.
*
* @param props The producer configuration map
*/
public ProducerConfig(Map<String, Object> props) {View on GitHub (pinned to c31c9215e1)
Solutions
- Set `value.serializer` to the fully-qualified name of an appropriate Serializer, e.g. org.apache.kafka.common.serialization.StringSerializer.
- Alternatively pass a Serializer<?> instance as the third argument to the KafkaProducer constructor.
- For Avro/Protobuf/JSON, use the appropriate framework serializer class.
Example fix
// before
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
new KafkaProducer<String,String>(props);
// after
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
new KafkaProducer<String,String>(props); Defensive patterns
Strategy: type-guard
Validate before calling
// Ensure a value serializer is present BEFORE constructing KafkaProducer.
static void ensureValueSerializer(Map<String, Object> cfg, Serializer<?> valSer) {
if (valSer != null) return;
Object cls = cfg.get(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG);
if (cls == null || (cls instanceof String && ((String) cls).isEmpty()))
throw new IllegalArgumentException("value.serializer must be set (class or instance)");
}
// Caller:
ensureValueSerializer(cfg, valueSerializer); Type guard
// Narrow to a non-null value-serializer guarantee.
static boolean hasValueSerializer(Map<String, Object> cfg, Serializer<?> instance) {
if (instance != null) return true;
Object v = cfg.get(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG);
return v instanceof Class || (v instanceof String && !((String) v).isEmpty());
}
// Use: assert hasValueSerializer(cfg, valueSerializer); Try / catch
try {
producer = new KafkaProducer<>(cfg, keySerializer, valueSerializer);
} catch (ConfigException e) {
if (e.getMessage().contains("value.serializer")) {
valueSerializer = new org.apache.kafka.common.serialization.ByteArraySerializer();
producer = new KafkaProducer<>(cfg, keySerializer, valueSerializer);
} else throw e;
} Prevention
- Always set value.serializer.class or pass a Serializer<ValueType> instance matching your value type.
- Use KafkaProducer<K,V> generics so the compiler ties the serializer to the value type.
- When loading config from a file, fail fast on a missing serializer rather than letting the producer constructor reject it later.
When it happens
Trigger: Constructing `new KafkaProducer<>(props)` where props has no `value.serializer` entry AND no value Serializer instance is passed; or constructing `new KafkaProducer<>(props, keySerializer, null)`.
Common situations: First-time producer setup forgetting the value.serializer property; loading properties from a file that was only partially populated; mixing constructor-supplied key serializer with config-supplied value serializer and forgetting the latter.
Related errors
- Invalid value null for configuration key.serializer: must be
- Invalid producer ID and epoch values: {producerId}:{epoch}.
- Invalid serialized transaction state format: {serializedStat
- Must set retries to non-zero when using the idempotent produ
- Must set acks to all in order to use the idempotent producer
AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03).
Data as JSON: /data/errors/352e936e7df8c185.json.
Report an issue: GitHub.