apache/flink · error · NullPointerException
Record to serialize to byte array must not be null.
Error message
Record to serialize to byte array must not be null.
What it means
InstantiationUtil.serializeToByteArray(serializer, record) explicitly rejects a null record with NullPointerException('Record to serialize to byte array must not be null.') before handing the pair to the TypeSerializer. The guard exists because many serializers would otherwise NPE deep inside serialization with an unhelpful trace; failing fast names the actual contract violation.
Source
Thrown at flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java:434
throws IOException, ClassNotFoundException {
byte[] bytes = config.getBytes(key, null);
if (bytes == null) {
return null;
}
return deserializeObject(bytes, cl);
}
public static void writeObjectToConfig(Object o, Configuration config, String key)
throws IOException {
byte[] bytes = serializeObject(o);
config.setBytes(key, bytes);
}
public static <T> byte[] serializeToByteArray(TypeSerializer<T> serializer, T record)
throws IOException {
if (record == null) {
throw new NullPointerException("Record to serialize to byte array must not be null.");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(64);
DataOutputViewStreamWrapper outputViewWrapper = new DataOutputViewStreamWrapper(bos);
serializer.serialize(record, outputViewWrapper);
return bos.toByteArray();
}
public static <T> T deserializeFromByteArray(TypeSerializer<T> serializer, byte[] buf)
throws IOException {
if (buf == null) {
throw new NullPointerException("Byte array to deserialize from must not be null.");
}
DataInputViewStreamWrapper inputViewWrapper =
new DataInputViewStreamWrapper(new ByteArrayInputStream(buf));
return serializer.deserialize(inputViewWrapper);
}View on GitHub (pinned to 2f3c205e92)
Solutions
- Filter or map nulls to a sentinel/absent representation before serializing
- Fix the producer so records are never null — Flink dataflow does not support null elements
- If null is meaningful in your own storage layer, branch: store a null marker byte instead of calling this utility
Example fix
// before
byte[] bytes = InstantiationUtil.serializeToByteArray(serializer, maybeNull);
// after
if (maybeNull == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.write(InstantiationUtil.serializeToByteArray(serializer, maybeNull));
} Defensive patterns
Strategy: validation
Validate before calling
java.util.Objects.requireNonNull(record, "record"); // fail with your own message first
Prevention
- Never emit null records from user functions
- Model optional data with a sentinel or a presence flag, not null
When it happens
Trigger: Calling serializeToByteArray with a null element — e.g. serializing the result of a lookup/flatMap that produced null, or a default value that was never initialized.
Common situations: User functions returning null (Flink disallows null elements in most pipelines); optional fields collapsed into whole-record null; tests passing null placeholders.
Related errors
- Byte array to deserialize from must not be null.
- Unsupported serializer type %s for %s
- Class is not a TypeInfoFactory.
- Invalid bounds.
- Length may not be negative.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/1a87d1010a6ca41e.
Report an issue: GitHub.