zhisheng17/flink-learning · error · IllegalArgumentException

bytes == null

Error message

bytes == null

What it means

StringUtils.byteToHexString converts a byte array (or a range of it) to a hex string. It explicitly rejects a null array with IllegalArgumentException('bytes == null') rather than returning null, since a null has no hex representation.

Source

Thrown at flink-learning-core/src/main/java/com/zhisheng/core/utils/StringUtils.java:59

public final class StringUtils {

	private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	/**
	 * Given an array of bytes it will convert the bytes to a hex string
	 * representation of the bytes.
	 *
	 * @param bytes
	 *        the bytes to convert in a hex string
	 * @param start
	 *        start index, inclusively
	 * @param end
	 *        end index, exclusively
	 * @return hex string representation of the byte array
	 */
	public static String byteToHexString(final byte[] bytes, final int start, final int end) {
		if (bytes == null) {
			throw new IllegalArgumentException("bytes == null");
		}

		int length = end - start;
		char[] out = new char[length * 2];

		for (int i = start, j = 0; i < end; i++) {
			out[j++] = HEX_CHARS[(0xF0 & bytes[i]) >>> 4];
			out[j++] = HEX_CHARS[0x0F & bytes[i]];
		}

		return new String(out);
	}

	/**
	 * Given an array of bytes it will convert the bytes to a hex string
	 * representation of the bytes.
	 *
	 * @param bytes

View on GitHub (pinned to d731cee761)

Solutions

  1. Null-check the byte array before calling byteToHexString
  2. Ensure the producer of the bytes returns an empty array instead of null
  3. Return an empty string for null at your call site if that is the desired semantics

Example fix

// before
String hex = StringUtils.byteToHexString(bytes, 0, bytes.length);
// after
String hex = bytes == null ? "" : StringUtils.byteToHexString(bytes, 0, bytes.length);
Defensive patterns

Strategy: type-guard

Validate before calling

if (bytes == null) {
    throw new IllegalArgumentException("byte array must not be null before hex encoding");
}

Type guard

static boolean isNonNullBytes(byte[] b) {
    return b != null;
}

Try / catch

try {
    String hex = StringUtils.byteToHexString(bytes, 0, bytes.length);
} catch (IllegalArgumentException e) {
    if ("bytes == null".equals(e.getMessage())) {
        hex = ""; // or handle absent payload explicitly
    } else throw e;
}

Prevention

When it happens

Trigger: Calling byteToHexString(null, start, end) or byteToHexString(null) where the byte array reference is null — typically the result of a method that returned null (e.g. digest/decode call) passed straight through.

Common situations: Hashing utilities receiving null input from optional fields, deserialized records with missing byte payloads, or APIs returning null instead of empty arrays.

Related errors


AI-assisted analysis of zhisheng17/flink-learning@d731cee761 (2026-09-06). Data as JSON: /api/errors/189741baf08220b0. Report an issue: GitHub.