{"record":{"id":"1a87d1010a6ca41e","repo":"apache/flink","slug":"record-to-serialize-to-byte-array-must-not-be-null","errorCode":null,"errorMessage":"Record to serialize to byte array must not be null.","messagePattern":"Record to serialize to byte array must not be null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java","lineNumber":434,"sourceCode":"            throws IOException, ClassNotFoundException {\n        byte[] bytes = config.getBytes(key, null);\n        if (bytes == null) {\n            return null;\n        }\n\n        return deserializeObject(bytes, cl);\n    }\n\n    public static void writeObjectToConfig(Object o, Configuration config, String key)\n            throws IOException {\n        byte[] bytes = serializeObject(o);\n        config.setBytes(key, bytes);\n    }\n\n    public static <T> byte[] serializeToByteArray(TypeSerializer<T> serializer, T record)\n            throws IOException {\n        if (record == null) {\n            throw new NullPointerException(\"Record to serialize to byte array must not be null.\");\n        }\n\n        ByteArrayOutputStream bos = new ByteArrayOutputStream(64);\n        DataOutputViewStreamWrapper outputViewWrapper = new DataOutputViewStreamWrapper(bos);\n        serializer.serialize(record, outputViewWrapper);\n        return bos.toByteArray();\n    }\n\n    public static <T> T deserializeFromByteArray(TypeSerializer<T> serializer, byte[] buf)\n            throws IOException {\n        if (buf == null) {\n            throw new NullPointerException(\"Byte array to deserialize from must not be null.\");\n        }\n\n        DataInputViewStreamWrapper inputViewWrapper =\n                new DataInputViewStreamWrapper(new ByteArrayInputStream(buf));\n        return serializer.deserialize(inputViewWrapper);\n    }","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java#L416-L452","documentation":"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.","triggerScenarios":"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.","commonSituations":"User functions returning null (Flink disallows null elements in most pipelines); optional fields collapsed into whole-record null; tests passing null placeholders.","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"],"exampleFix":"// before\nbyte[] bytes = InstantiationUtil.serializeToByteArray(serializer, maybeNull);\n// after\nif (maybeNull == null) {\n    out.writeBoolean(false);\n} else {\n    out.writeBoolean(true);\n    out.write(InstantiationUtil.serializeToByteArray(serializer, maybeNull));\n}","handlingStrategy":"validation","validationCode":"java.util.Objects.requireNonNull(record, \"record\"); // fail with your own message first","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never emit null records from user functions","Model optional data with a sentinel or a presence flag, not null"],"tags":["serialization","null-handling","validation"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}