{"record":{"id":"0216818781ec1a3d","repo":"perwendel/spark","slug":"httpservletrequest-cannot-be-null","errorCode":null,"errorMessage":"HttpServletRequest cannot be null.","messagePattern":"HttpServletRequest cannot be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/spark/QueryParamsMap.java","lineNumber":60,"sourceCode":"     * Holds the nested keys\n     */\n    private Map<String, QueryParamsMap> queryMap = new HashMap<>();\n\n    /**\n     * Value(s) for this key\n     */\n    private String[] values;\n\n    /**\n     * Creates a new QueryParamsMap from an HttpServletRequest. <br>\n     * Parses the parameters from request.getParameterMap() <br>\n     * No need to decode, since HttpServletRequest does it for us.\n     *\n     * @param request the servlet request\n     */\n    public QueryParamsMap(HttpServletRequest request) {\n        if (request == null) {\n            throw new IllegalArgumentException(\"HttpServletRequest cannot be null.\");\n        }\n        loadQueryString(request.getParameterMap());\n    }\n\n    // Just for testing\n    protected QueryParamsMap() {\n    }\n\n\n    /**\n     * Parses the key and creates the child QueryParamMaps\n     * user[info][name] creates 3 nested QueryParamMaps. For user, info and\n     * name.\n     *\n     * @param key    The key in the formar fo key1[key2][key3] (for example:\n     *               user[info][name]).\n     * @param values the values\n     */","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/perwendel/spark/blob/1973e402f5d4c1442ad34a1d38ed0758079f7773/src/main/java/spark/QueryParamsMap.java#L42-L78","documentation":"Spark's QueryParamsMap constructor wraps an HttpServletRequest and exposes its query parameters as a nested map. The constructor explicitly requires a non-null request because it immediately delegates to loadQueryString(request.getParameterMap()); there is no meaningful way to parse query params without a request. Passing null is a programming error, so the constructor fails fast with IllegalArgumentException.","triggerScenarios":"Calling new QueryParamsMap(null) directly, or passing a null/unset HttpServletRequest variable into the constructor, typically in unit tests or custom filter code where the request object was never assigned.","commonSituations":"Unit tests constructing QueryParamsMap with a stub that was never wired up; custom wrappers around Spark that resolve the request lazily and get null outside of a request scope; refactoring that removed request initialization.","solutions":["Pass the actual HttpServletRequest from the request-handling scope (e.g. inside a Route/Filter handler use the request parameter, or Request.raw()).","In tests, provide a mock or stub HttpServletRequest (Mockito mock, or Spark's embedded test helpers) instead of null.","Guard the call site: only construct QueryParamsMap when a request is actually available; never call it outside a live request context."],"exampleFix":"// before\nQueryParamsMap qpm = new QueryParamsMap(request); // request is null\n// after\nif (request != null) {\n    QueryParamsMap qpm = new QueryParamsMap(request);\n} else {\n    throw new IllegalStateException(\"QueryParamsMap requires an active HttpServletRequest\");\n}","handlingStrategy":"validation","validationCode":"if (request == null) {\n    throw new IllegalStateException(\"Cannot build QueryParamsMap: no HttpServletRequest available\");\n}\nQueryParamsMap qpm = new QueryParamsMap(request);","typeGuard":"boolean hasRequest(javax.servlet.http.HttpServletRequest r) { return r != null; }","tryCatchPattern":"try {\n    QueryParamsMap qpm = new QueryParamsMap(request);\n} catch (IllegalArgumentException e) {\n    // request was null — fall back to empty map or rethrow with context\n    QueryParamsMap qpm = new QueryParamsMap(new java.util.HashMap<>());\n}","preventionTips":["Only construct QueryParamsMap inside a live request scope (Route/Filter handler).","Use Objects.requireNonNull(request) at the top of wrapper methods for a clearer stack trace.","In tests, use Mockito.mock(HttpServletRequest.class) or Spark's test helpers instead of null stubs."],"tags":["java","null-argument","validation"],"backgroundTag":"null-argument","analyzedSha":"1973e402f5d4c1442ad34a1d38ed0758079f7773","analyzedAt":"2026-09-10T14:38:22.866Z","contentChangedAt":"2026-09-10T14:38:22.866Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}