hibernate/hibernate-orm · error · IllegalArgumentException

The OutputStream must not be null

Error message

The OutputStream must not be null

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java:96

	/**
	 * <p>Serializes an object to the given stream.
	 * <p>
	 * The stream will be closed once the object is written.
	 * This avoids the need for a finally clause, and maybe also
	 * for exception handling, in the application code.
	 * <p>
	 * The stream passed in is not buffered internally within this
	 * method. This is the responsibility of the caller, if desired.
	 *
	 * @param obj the object to serialize to bytes, may be null
	 * @param outputStream the stream to write to, must not be null
	 *
	 * @throws IllegalArgumentException if {@code outputStream} is null
	 * @throws SerializationException (runtime) if the serialization fails
	 */
	public static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException {
		if ( outputStream == null ) {
			throw new IllegalArgumentException( "The OutputStream must not be null" );
		}

		if ( CORE_LOGGER.isTraceEnabled() ) {
			if ( Hibernate.isInitialized( obj ) ) {
				CORE_LOGGER.tracev( "Starting serialization of object [{0}]", obj );
			}
			else {
				CORE_LOGGER.trace( "Starting serialization of [uninitialized proxy]" );
			}
		}

		try ( var out = new ObjectOutputStream( outputStream ) ) {
			out.writeObject( obj );
		}
		catch (IOException ex) {
			throw new SerializationException( "could not serialize", ex );
		}
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Pass a real OutputStream (e.g., new ByteArrayOutputStream()) and null-check the variable at your own API boundary first.
  2. If serialization is genuinely optional at that call site, branch on null and skip the call instead of passing it through.

Example fix

// before
OutputStream os = findStream(); // may return null
SerializationHelper.serialize(data, os); // IllegalArgumentException

// after
OutputStream os = findStream();
if (os == null) throw new IllegalStateException("destination stream unavailable");
SerializationHelper.serialize(data, os);
Defensive patterns

Strategy: validation

Validate before calling

if (outputStream == null) {
    throw new IllegalStateException("destination stream unavailable");
}
SerializationHelper.serialize(data, outputStream);

Prevention

When it happens

Trigger: Calling SerializationHelper.serialize(obj, null), typically because stream construction was skipped, a lookup helper returned null, or the argument variable was never initialized.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/a0e22473b3c522d3. Report an issue: GitHub.