{"id":"352e936e7df8c185","repo":"apache/kafka","slug":"invalid-value-null-for-configuration-value-seriali","errorCode":null,"errorMessage":"Invalid value null for configuration value.serializer: must be non-null.","messagePattern":"Invalid value null for configuration value\\.serializer: must be non-null\\.","errorType":"validation","errorClass":"ConfigException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/producer/ProducerConfig.java","lineNumber":725,"sourceCode":"            return acksString.trim().equalsIgnoreCase(\"all\") ? \"-1\" : Short.parseShort(acksString.trim()) + \"\";\n        } catch (NumberFormatException e) {\n            throw new ConfigException(\"Invalid configuration value for 'acks': \" + acksString);\n        }\n    }\n\n    static Map<String, Object> appendSerializerToConfig(Map<String, Object> configs,\n            Serializer<?> keySerializer,\n            Serializer<?> valueSerializer) {\n        // validate serializer configuration, if the passed serializer instance is null, the user must explicitly set a valid serializer configuration value\n        Map<String, Object> newConfigs = new HashMap<>(configs);\n        if (keySerializer != null)\n            newConfigs.put(KEY_SERIALIZER_CLASS_CONFIG, keySerializer.getClass());\n        else if (newConfigs.get(KEY_SERIALIZER_CLASS_CONFIG) == null)\n            throw new ConfigException(KEY_SERIALIZER_CLASS_CONFIG, null, \"must be non-null.\");\n        if (valueSerializer != null)\n            newConfigs.put(VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer.getClass());\n        else if (newConfigs.get(VALUE_SERIALIZER_CLASS_CONFIG) == null)\n            throw new ConfigException(VALUE_SERIALIZER_CLASS_CONFIG, null, \"must be non-null.\");\n        return newConfigs;\n    }\n\n    /**\n     * Constructs a new ProducerConfig with the given properties.\n     *\n     * @param props The producer configuration properties\n     */\n    public ProducerConfig(Properties props) {\n        super(CONFIG, props);\n    }\n\n    /**\n     * Constructs a new ProducerConfig with the given configuration map.\n     *\n     * @param props The producer configuration map\n     */\n    public ProducerConfig(Map<String, Object> props) {","sourceCodeStart":707,"sourceCodeEnd":743,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerConfig.java#L707-L743","documentation":"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.","triggerScenarios":"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)`.","commonSituations":"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.","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."],"exampleFix":"// before\nProperties props = new Properties();\nprops.put(\"bootstrap.servers\", \"localhost:9092\");\nprops.put(\"key.serializer\", \"org.apache.kafka.common.serialization.StringSerializer\");\nnew KafkaProducer<String,String>(props);\n\n// after\nProperties props = new Properties();\nprops.put(\"bootstrap.servers\", \"localhost:9092\");\nprops.put(\"key.serializer\", \"org.apache.kafka.common.serialization.StringSerializer\");\nprops.put(\"value.serializer\", \"org.apache.kafka.common.serialization.StringSerializer\");\nnew KafkaProducer<String,String>(props);","handlingStrategy":"type-guard","validationCode":"// Ensure a value serializer is present BEFORE constructing KafkaProducer.\nstatic void ensureValueSerializer(Map<String, Object> cfg, Serializer<?> valSer) {\n    if (valSer != null) return;\n    Object cls = cfg.get(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG);\n    if (cls == null || (cls instanceof String && ((String) cls).isEmpty()))\n        throw new IllegalArgumentException(\"value.serializer must be set (class or instance)\");\n}\n// Caller:\nensureValueSerializer(cfg, valueSerializer);","typeGuard":"// Narrow to a non-null value-serializer guarantee.\nstatic boolean hasValueSerializer(Map<String, Object> cfg, Serializer<?> instance) {\n    if (instance != null) return true;\n    Object v = cfg.get(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG);\n    return v instanceof Class || (v instanceof String && !((String) v).isEmpty());\n}\n// Use: assert hasValueSerializer(cfg, valueSerializer);","tryCatchPattern":"try {\n    producer = new KafkaProducer<>(cfg, keySerializer, valueSerializer);\n} catch (ConfigException e) {\n    if (e.getMessage().contains(\"value.serializer\")) {\n        valueSerializer = new org.apache.kafka.common.serialization.ByteArraySerializer();\n        producer = new KafkaProducer<>(cfg, keySerializer, valueSerializer);\n    } else throw e;\n}","preventionTips":["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."],"tags":["kafka","producer","configuration","serialization"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}