apache/flink · error · NullPointerException

Cannot register null class or serializer.

Error message

Cannot register null class or serializer.

What it means

Thrown by addDefaultKryoSerializer(Class<?> type, T serializer) when either the type class or the serializer instance is null. Registering a Kryo default serializer requires both a non-null target class and a non-null serializer object; a null argument indicates a caller bug. This is a hard precondition enforced before any state mutation.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/serialization/SerializerConfigImpl.java:104

    @Internal
    public SerializerConfigImpl(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * Adds a new Kryo default serializer to the Runtime.
     *
     * <p>Note that the serializer instance must be serializable (as defined by
     * java.io.Serializable), because it may be distributed to the worker nodes by java
     * serialization.
     *
     * @param type The class of the types serialized with the given serializer.
     * @param serializer The serializer to use.
     */
    public <T extends Serializer<?> & Serializable> void addDefaultKryoSerializer(
            Class<?> type, T serializer) {
        if (type == null || serializer == null) {
            throw new NullPointerException("Cannot register null class or serializer.");
        }

        defaultKryoSerializers.put(type, new SerializableSerializer<>(serializer));
    }

    /**
     * Adds a new Kryo default serializer to the Runtime.
     *
     * @param type The class of the types serialized with the given serializer.
     * @param serializerClass The class of the serializer to use.
     */
    public void addDefaultKryoSerializer(
            Class<?> type, Class<? extends Serializer<?>> serializerClass) {
        if (type == null || serializerClass == null) {
            throw new NullPointerException("Cannot register null class or serializer.");
        }
        defaultKryoSerializerClasses.put(type, serializerClass);
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Check that the type Class<?> argument is non-null before calling the method.
  2. Check that the serializer instance is non-null and properly instantiated.
  3. If loading the class dynamically, guard against null and log a meaningful error before calling addDefaultKryoSerializer.

Example fix

// before
config.addDefaultKryoSerializer(loadedClass, null);

// after
if (loadedClass != null && serializer != null) {
    config.addDefaultKryoSerializer(loadedClass, serializer);
}
Defensive patterns

Strategy: validation

Validate before calling

if (type == null || serializer == null) {
    throw new IllegalArgumentException("type and serializer must both be non-null");
}
config.addDefaultKryoSerializer(type, serializer);

Type guard

import java.util.Objects;
Objects.requireNonNull(type, "type");
Objects.requireNonNull(serializer, "serializer");

Prevention

When it happens

Trigger: Calling env.getConfig().addDefaultKryoSerializer(null, mySerializer) or addDefaultKryoSerializer(MyClass.class, null). Common when the type or serializer is looked up dynamically and the lookup returned null.

Common situations: Dynamic class loading where Class.forName returned null or threw silently; passing a serializer field that was never initialized; copy-paste errors wiring configuration.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/fd3a983804ebc16b. Report an issue: GitHub.