jhy/jsoup · error · ValidationException

String must not be empty

Error message

String must not be empty

What it means

Validate.notEmpty(string) asserts that a string is both non-null and has length greater than zero, throwing ValidationException("String must not be empty") otherwise. jsoup uses it wherever an empty string would be meaningless or dangerous (e.g. tag names, selectors, keys).

Solutions

  1. Trim and check for emptiness before the call; reject blank input with a clear user-facing message
  2. Provide a sensible default for optional string inputs instead of an empty string
  3. Use notEmpty(string, msg) in your own code so failures name the offending field

Example fix

// before
String selector = input.trim(); // ""
doc.select(selector);
// after
String selector = input.trim();
if (!selector.isEmpty()) { doc.select(selector); }
else { throw new IllegalArgumentException("selector must not be blank"); }
Defensive patterns

Strategy: validation

Validate before calling

String s = raw == null ? "" : raw.trim();
if (s.isEmpty()) {
    throw new IllegalArgumentException("selector must not be blank");
}

Type guard

boolean isBlank(String s) { return s == null || s.trim().isEmpty(); }

Try / catch

try {
    doc.select(userSelector);
} catch (ValidationException e) {
    if (e.getMessage().equals("String must not be empty")) {
        promptUserForNonBlankInput();
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling a jsoup API guarded by notEmpty with "" or null — e.g. an empty selector string, empty tag name, empty attribute key, or a string built by concatenation/trimming that ended up empty.

Common situations: User-supplied search terms or selectors that were blank after trimming; config values read as empty strings from properties/env defaults; split() results containing empty tokens.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

     * 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
     */
    public static void notEmptyParam(@Nullable final String string, final String param) {
        if (string == null || string.length() == 0)
            throw new ValidationException(String.format("The '%s' parameter must not be empty.", param));
    }

    /**
     * Validates that the string is not null and is not empty
     * @param string the string to test
     * @param msg message to include in the Exception if validation fails
     * @throws ValidationException if the string is null or empty

View on GitHub (pinned to 9851ac5d9c)