{"record":{"id":"a0e22473b3c522d3","repo":"hibernate/hibernate-orm","slug":"the-outputstream-must-not-be-null","errorCode":null,"errorMessage":"The OutputStream must not be null","messagePattern":"The OutputStream must not be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java","lineNumber":96,"sourceCode":"\t/**\n\t * <p>Serializes an object to the given stream.\n\t * <p>\n\t * The stream will be closed once the object is written.\n\t * This avoids the need for a finally clause, and maybe also\n\t * for exception handling, in the application code.\n\t * <p>\n\t * The stream passed in is not buffered internally within this\n\t * method. This is the responsibility of the caller, if desired.\n\t *\n\t * @param obj the object to serialize to bytes, may be null\n\t * @param outputStream the stream to write to, must not be null\n\t *\n\t * @throws IllegalArgumentException if {@code outputStream} is null\n\t * @throws SerializationException (runtime) if the serialization fails\n\t */\n\tpublic static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException {\n\t\tif ( outputStream == null ) {\n\t\t\tthrow new IllegalArgumentException( \"The OutputStream must not be null\" );\n\t\t}\n\n\t\tif ( CORE_LOGGER.isTraceEnabled() ) {\n\t\t\tif ( Hibernate.isInitialized( obj ) ) {\n\t\t\t\tCORE_LOGGER.tracev( \"Starting serialization of object [{0}]\", obj );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tCORE_LOGGER.trace( \"Starting serialization of [uninitialized proxy]\" );\n\t\t\t}\n\t\t}\n\n\t\ttry ( var out = new ObjectOutputStream( outputStream ) ) {\n\t\t\tout.writeObject( obj );\n\t\t}\n\t\tcatch (IOException ex) {\n\t\t\tthrow new SerializationException( \"could not serialize\", ex );\n\t\t}\n\t}","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java#L78-L114","documentation":"SerializationHelper.serialize(Serializable, OutputStream) is null-hostile by contract: the destination stream must exist before serialization starts. Passing a null OutputStream triggers IllegalArgumentException immediately, before the object graph is touched. The null check is deliberately fail-fast rather than letting the JDK throw a less clear NPE later.","triggerScenarios":"Calling SerializationHelper.serialize(obj, null), typically because stream construction was skipped, a lookup helper returned null, or the argument variable was never initialized.","commonSituations":"Lazily created streams passed unconditionally; refactor leftovers where stream creation moved to another branch; test harnesses calling serialize with placeholder arguments; optional-output code paths that forget to branch.","solutions":["Pass a real OutputStream (e.g., new ByteArrayOutputStream()) and null-check the variable at your own API boundary first.","If serialization is genuinely optional at that call site, branch on null and skip the call instead of passing it through."],"exampleFix":"// before\nOutputStream os = findStream(); // may return null\nSerializationHelper.serialize(data, os); // IllegalArgumentException\n\n// after\nOutputStream os = findStream();\nif (os == null) throw new IllegalStateException(\"destination stream unavailable\");\nSerializationHelper.serialize(data, os);","handlingStrategy":"validation","validationCode":"if (outputStream == null) {\n    throw new IllegalStateException(\"destination stream unavailable\");\n}\nSerializationHelper.serialize(data, outputStream);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass lazily obtained streams unconditionally; branch on null first.","Make stream creation and use adjacent in code so the pairing is obvious.","Treat this helper's arguments as non-optional by contract."],"tags":["hibernate","serialization","null-check","argument-validation"],"backgroundTag":"null-argument-contract-violation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}