jhy/jsoup · error · ValidationException
Object must not be null
Error message
Object must not be null
What it means
Validate.notNull(obj) is jsoup's basic null assertion helper. It throws a ValidationException with the message "Object must not be null" whenever any jsoup API that guards its inputs receives a null object reference.
Solutions
- Check for null before calling the jsoup API, or use Optional to model the absence
- Use Validate.expectNotNull()/notNullParam() in your own code to fail fast with context at the call site
- Trace which argument was null by wrapping the call and logging inputs on failure
Example fix
// before
doc.select("div.main").first().html(); // NPE path -> ValidationException if null guarded
// after
Element main = doc.select("div.main").first();
if (main != null) System.out.println(main.html()); Defensive patterns
Strategy: validation
Validate before calling
if (element == null) {
// handle absence explicitly instead of passing null into the API
return Optional.empty();
} Type guard
Optional<Element> safe = Optional.ofNullable(doc.selectFirst("div.main")); Try / catch
try {
apiCall(maybeNull);
} catch (ValidationException e) {
if (e.getMessage().equals("Object must not be null")) {
log.error("Required object was null at call site", e);
} else { throw e; }
} Prevention
- Null-check results of first()/selectFirst() before use
- Wrap nullable lookups in Optional at boundaries
- Annotate parameters @NonNull and run static analysis
- Fail fast with Objects.requireNonNull and a descriptive message at your own API edges
When it happens
Trigger: Passing null to any jsoup method that internally calls Validate.notNull — e.g. a null Element, null tag name, null attribute key, or null selector string at an API boundary guarded by this helper.
Common situations: Chaining off a query that returned null (element.select(...).first() can be null); map lookups or optional unwrapping that yielded null before being handed to jsoup; version changes where an API that previously tolerated null became strict.
Related errors
- The parameter ' ' must not be null.
- msg
- The ' ' parameter must not be empty.
- Pattern syntax error:
- Pattern complexity error
AI-assisted analysis of jhy/jsoup@9851ac5d9c (2026-09-08).
Data as JSON: /api/errors/400ee9b80e314a3d.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/org/jsoup/helper/Validate.java:19
package org.jsoup.helper;
import org.jspecify.annotations.Nullable;
/**
* Validators to check that method arguments meet expectations.
*/
public final class Validate {
private Validate() {}
/**
* Validates that the object is not null
* @param obj object to test
* @throws ValidationException if the object is null
*/
public static void notNull(@Nullable Object obj) {
if (obj == null)
throw new ValidationException("Object must not be null");
}
/**
Validates that the parameter is not null
* @param obj the parameter to test
* @param param the name of the parameter, for presentation in the validation exception.
* @throws ValidationException if the object is null
*/
public static void notNullParam(@Nullable final Object obj, final String param) {
if (obj == null)
throw new ValidationException(String.format("The parameter '%s' must not be null.", param));
}
/**
* Validates that the object is not null
* @param obj object to test
* @param msg message to include in the Exception if validation failsView on GitHub (pinned to 9851ac5d9c)