perwendel/spark · error · IllegalArgumentException
httpMethod cannot be null or blank
Error message
httpMethod cannot be null or blank
What it means
Routes.remove(String path, String httpMethod) validates that the HTTP method string is non-null and non-blank before converting it via HttpMethod.valueOf. An empty or null httpMethod cannot map to a route, so Spark throws IllegalArgumentException.
Solutions
- Pass a valid HTTP method string: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "TRACE", "CONNECT", "OPTIONS".
- Guard the call against null/blank before invoking remove.
- Note the adjacent check: after this validation, HttpMethod.valueOf will also throw IllegalArgumentException for a non-blank but invalid method name, so use an exact uppercase method name.
Example fix
// before
routes.remove("/users", method); // method may be null/blank
// after
if (method != null && !method.trim().isEmpty()) {
routes.remove("/users", method.toUpperCase());
} Defensive patterns
Strategy: validation
Validate before calling
if (httpMethod == null || httpMethod.trim().isEmpty()) throw new IllegalArgumentException("httpMethod must be non-blank");
routes.remove(path, httpMethod.toUpperCase()); Try / catch
try {
routes.remove(path, httpMethod);
} catch (IllegalArgumentException e) {
log.error("Invalid httpMethod for route removal: {}", e.getMessage());
} Prevention
- Use HttpMethod enum names (uppercase) instead of arbitrary strings.
- Derive the method string from HttpMethod.valueOf-able constants.
When it happens
Trigger: Calling routes.remove("/path", null), routes.remove("/path", ""), or routes.remove("/path", " ").
Common situations: HTTP method read from a variable/config/request that is unset; uppercase/lowercase mishandling leaving an empty string after trimming; a switch statement building the method name incorrectly.
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
- path cannot be null or blank
- WebSocket handler must implement 'WebSocketListener' or be…
- The must start with "/" and end with "/*". It's
- There are no Spark applications configured in the filter.
- classpath
AI-assisted analysis of perwendel/spark@1973e402f5 (2026-09-10).
Data as JSON: /api/errors/6945eaba87eb3ff1.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/spark/route/Routes.java:155
/**
* 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
* @throws java.lang.IllegalArgumentException if <tt>path</tt> is null or blank
* @since 2.2
*/
public boolean remove(String path) {View on GitHub (pinned to 1973e402f5)