perwendel/spark · error · IllegalArgumentException

path cannot be null or blank

Error message

path cannot be null or blank

What it means

Routes.remove(String path, String httpMethod) removes a previously registered route by path plus HTTP method. Spark validates the path argument with StringUtils.isEmpty and throws IllegalArgumentException when it is null or blank, because a blank path cannot identify any route.

Solutions

  1. Pass a valid, non-blank route path (e.g. "/api/users") as the first argument.
  2. Guard the call: only invoke remove when the path variable is a non-empty string.
  3. Fix the source of the empty path (missing config property, unset field, wrong map key).

Example fix

// before
routes.remove(config.path(), "GET"); // throws if config.path() is null

// after
if (config.path() != null && !config.path().trim().isEmpty()) {
    routes.remove(config.path(), "GET");
}
Defensive patterns

Strategy: validation

Validate before calling

if (path == null || path.trim().isEmpty()) throw new IllegalArgumentException("path must be non-blank");
routes.remove(path, httpMethod);

Try / catch

try {
    routes.remove(path, httpMethod);
} catch (IllegalArgumentException e) {
    log.error("Invalid route removal: {}", e.getMessage());
}

Prevention

When it happens

Trigger: Calling routes.remove(null, "GET"), routes.remove("", "GET"), or routes.remove(" ", "GET") on the Routes instance (e.g. via Spark's static removeRouteMapping).

Common situations: Path built dynamically from a config value or request parameter that is unset/empty; refactoring where the path variable was dropped; storing routes in a map where a missing key yields null.

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 perwendel/spark@1973e402f5 (2026-09-10). Data as JSON: /api/errors/d2c5bdff1e5d2a8b. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/spark/route/Routes.java:151

    public void clear() {
        routes.clear();
    }

    /**
     * Removes a particular route from the collection of those that have been previously routed.
     * Search for a previously established routes using the given path and HTTP method, removing
     * any matches that are found.
     *
     * @param path       the route path
     * @param httpMethod the http method
     * @return <tt>true</tt> if this a matching route has been previously routed
     * @throws IllegalArgumentException if <tt>path</tt> is null or blank or if <tt>httpMethod</tt> is null, blank
     *                                  or an invalid HTTP method
     * @since 2.2
     */
    public boolean remove(String path, String httpMethod) {
        if (StringUtils.isEmpty(path)) {
            throw new IllegalArgumentException("path cannot be null or blank");
        }

        if (StringUtils.isEmpty(httpMethod)) {
            throw new IllegalArgumentException("httpMethod cannot be null or blank");
        }

        // Catches invalid input and throws IllegalArgumentException
        HttpMethod method = HttpMethod.valueOf(httpMethod);

        return removeRoute(method, path);
    }

    /**
     * Removes a particular route from the collection of those that have been previously routed.
     * Search for a previously established routes using the given path and removes any matches that are found.
     *
     * @param path the route path
     * @return <tt>true</tt> if this a matching route has been previously routed

View on GitHub (pinned to 1973e402f5)