apache/flink · error · NullPointerException

Bytes must not be null

Error message

Bytes must not be null

What it means

setValueAscii(byte[] bytes, int offset, int len) interprets raw bytes as ASCII chars to set the StringValue's content. It explicitly rejects a null bytes array with NullPointerException ('Bytes must not be null') before any range logic, unlike its sibling setters which rely on checkNotNull elsewhere.

Source

Thrown at flink-core/src/main/java/org/apache/flink/types/StringValue.java:257

        ensureSize(len);
        System.arraycopy(chars, offset, this.value, 0, len);
        this.len = len;
        this.hashCode = 0;
    }

    /**
     * Sets the value of this <code>StringValue</code>, assuming that the binary data is ASCII
     * coded. The n-th character of the <code>StringValue</code> corresponds directly to the n-th
     * byte in the given array after the offset.
     *
     * @param bytes The binary character data.
     * @param offset The offset in the array.
     * @param len The number of bytes to read from the array.
     */
    public void setValueAscii(byte[] bytes, int offset, int len) {
        if (bytes == null) {
            throw new NullPointerException("Bytes must not be null");
        }
        if (len < 0 || offset < 0 || offset > bytes.length - len) {
            throw new IndexOutOfBoundsException();
        }

        ensureSize(len);
        this.len = len;
        this.hashCode = 0;

        final char[] chars = this.value;

        for (int i = 0, limit = offset + len; offset < limit; offset++, i++) {
            chars[i] = (char) (bytes[offset] & 0xff);
        }
    }

    // --------------------------------------------------------------------------------------------
    //                                    String Methods

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Null-check the array before calling: if (bytes == null) use setValue("") or skip the field
  2. Fix the producer so binary fields are never null (use empty arrays)
  3. Annotate/annotate-by-convention the parameter as non-null in your wrapper API

Example fix

// before
sv.setValueAscii(map.get(key), 0, n); // NPE when key absent

// after
byte[] b = map.get(key);
if (b == null) {
    sv.setValue("");
} else {
    sv.setValueAscii(b, 0, n);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (bytes == null) {
    sv.setValue("");
} else {
    sv.setValueAscii(bytes, offset, len);
}

Type guard

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

Prevention

When it happens

Trigger: Calling setValueAscii(null, off, len) — e.g. a lookup/decode step returned null for a missing key and the result was passed straight in.

Common situations: Map.get() or cache lookups feeding the setter without a null check; optional binary fields deserialized as null; test fixtures omitting the array.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/3bc2f2fb90b1920b. Report an issue: GitHub.