{"record":{"id":"400ee9b80e314a3d","repo":"jhy/jsoup","slug":"object-must-not-be-null","errorCode":null,"errorMessage":"Object must not be null","messagePattern":"Object must not be null","errorType":"validation","errorClass":"ValidationException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/jsoup/helper/Validate.java","lineNumber":19,"sourceCode":"package org.jsoup.helper;\n\nimport org.jspecify.annotations.Nullable;\n\n/**\n * Validators to check that method arguments meet expectations. \n */\npublic final class Validate {\n    \n    private Validate() {}\n\n    /**\n     * Validates that the object is not null\n     * @param obj object to test\n     * @throws ValidationException if the object is null\n     */\n    public static void notNull(@Nullable Object obj) {\n        if (obj == null)\n            throw new ValidationException(\"Object must not be null\");\n    }\n\n    /**\n     Validates that the parameter is not null\n\n     * @param obj the parameter to test\n     * @param param the name of the parameter, for presentation in the validation exception.\n     * @throws ValidationException if the object is null\n     */\n    public static void notNullParam(@Nullable final Object obj, final String param) {\n        if (obj == null)\n            throw new ValidationException(String.format(\"The parameter '%s' must not be null.\", param));\n    }\n\n    /**\n     * Validates that the object is not null\n     * @param obj object to test\n     * @param msg message to include in the Exception if validation fails","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/jhy/jsoup/blob/9851ac5d9c576c6888910b5a51a2362bbc978959/src/main/java/org/jsoup/helper/Validate.java#L1-L37","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ndoc.select(\"div.main\").first().html(); // NPE path -> ValidationException if null guarded\n// after\nElement main = doc.select(\"div.main\").first();\nif (main != null) System.out.println(main.html());","handlingStrategy":"validation","validationCode":"if (element == null) {\n    // handle absence explicitly instead of passing null into the API\n    return Optional.empty();\n}","typeGuard":"Optional<Element> safe = Optional.ofNullable(doc.selectFirst(\"div.main\"));","tryCatchPattern":"try {\n    apiCall(maybeNull);\n} catch (ValidationException e) {\n    if (e.getMessage().equals(\"Object must not be null\")) {\n        log.error(\"Required object was null at call site\", e);\n    } else { throw e; }\n}","preventionTips":["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"],"tags":["null-check","validation"],"backgroundTag":"null-argument","analyzedSha":"9851ac5d9c576c6888910b5a51a2362bbc978959","analyzedAt":"2026-09-08T15:22:04.931Z","contentChangedAt":"2026-09-08T15:22:04.931Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}