jhy/jsoup · error · ValidationException

msg

Error message

msg

What it means

Validate.noNullElements(objects, msg) iterates an array and throws ValidationException with the caller-supplied msg if any element is null. The message is passed in by the caller, so the thrown text ('msg' here) is whatever the guarding code specified.

Solutions

  1. Pre-filter the array (e.g. with a stream filter(Objects::nonNull)) before passing it
  2. Validate each element at its source so nulls never enter the array
  3. Include a descriptive msg so failures identify which collection contained the null

Example fix

// before
process(elements); // elements[1] is null
// after
Element[] safe = Arrays.stream(elements).filter(Objects::nonNull).toArray(Element[]::new);
process(safe);
Defensive patterns

Strategy: validation

Validate before calling

Object[] safe = Arrays.stream(objects).filter(Objects::nonNull).toArray();
if (safe.length != objects.length) {
    log.warn("Dropped {} null elements", objects.length - safe.length);
}

Try / catch

try {
    bulkOperation(objects);
} catch (ValidationException e) {
    // msg is caller-supplied; identify the collection from it and retry without nulls
    bulkOperation(Arrays.stream(objects).filter(Objects::nonNull).toArray());
}

Prevention

When it happens

Trigger: Passing an array or varargs collection to a jsoup API guarded by noNullElements where at least one slot is null — e.g. a null entry among multiple nodes, attributes, or strings being validated in bulk.

Common situations: Building varargs/arrays programmatically where an upstream null slipped in; filtering that produced sparse arrays; forwarding nullable collection elements into APIs that validate whole arrays.

Related errors


AI-assisted analysis of jhy/jsoup@9851ac5d9c (2026-09-08). Data as JSON: /api/errors/9796ab3014151487. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/org/jsoup/helper/Validate.java:135

    /**
     * Validates that the array contains no null elements
     * @param objects the array to test
     * @throws ValidationException if the array contains a null element
     */
    public static void noNullElements(Object[] objects) {
        noNullElements(objects, "Array must not contain any null objects");
    }

    /**
     * Validates that the array contains no null elements
     * @param objects the array to test
     * @param msg message to include in the Exception if validation fails
     * @throws ValidationException if the array contains a null element
     */
    public static void noNullElements(Object[] objects, String msg) {
        for (Object obj : objects)
            if (obj == null)
                throw new ValidationException(msg);
    }

    /**
     * Validates that the string is not null and is not empty
     * @param string the string to test
     * @throws ValidationException if the string is null or empty
     */
    public static void notEmpty(@Nullable String string) {
        if (string == null || string.length() == 0)
            throw new ValidationException("String must not be empty");
    }

    /**
     Validates that the string parameter is not null and is not empty
     * @param string the string to test
     * @param param the name of the parameter, for presentation in the validation exception.
     * @throws ValidationException if the string is null or empty
     */

View on GitHub (pinned to 9851ac5d9c)