{"record":{"id":"50c138ae7b3f3e89","repo":"apache/flink","slug":"byte-array-to-deserialize-from-must-not-be-null","errorCode":null,"errorMessage":"Byte array to deserialize from must not be null.","messagePattern":"Byte array to deserialize from 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":446,"sourceCode":"        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    }\n\n    public static <T> T deserializeFromByteArray(TypeSerializer<T> serializer, T reuse, 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(reuse, inputViewWrapper);\n    }\n","sourceCodeStart":428,"sourceCodeEnd":464,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java#L428-L464","documentation":"First overload of InstantiationUtil.deserializeFromByteArray(serializer, buf): it requires a non-null byte array and throws NullPointerException('Byte array to deserialize from must not be null.') otherwise. It then wraps buf in a DataInputViewStreamWrapper and calls serializer.deserialize — so the null check is the only pre-validation; a zero-length or corrupt array surfaces as whatever the serializer throws.","triggerScenarios":"Calling deserializeFromByteArray(serializer, null) — e.g. reading an optional field from state/config where the key was absent and getBytes returned null, or a map lookup that missed.","commonSituations":"Configuration round-trips where writeObjectToConfig never ran; absent cache entries; defaulting to null instead of an empty array when no data exists.","solutions":["Null-check the byte array at the source and supply empty/absent semantics explicitly","Use Configuration.containsKey/getOptional so absent values never reach deserialization as null","When storing optional payloads, write a presence flag (or use empty byte[0]) instead of null"],"exampleFix":"// before\nbyte[] bytes = config.getBytes(key);\nT value = InstantiationUtil.deserializeFromByteArray(ser, bytes);\n// after\nbyte[] bytes = config.getBytes(key);\nT value = (bytes == null) ? null : InstantiationUtil.deserializeFromByteArray(ser, bytes);","handlingStrategy":"validation","validationCode":"if (bytes == null) return defaultValue; // before calling deserializeFromByteArray","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write a presence marker when persisting optional payloads","Use getOptional/containsKey instead of assuming keys exist"],"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"}