{"record":{"id":"7f8a76c47210068d","repo":"alibaba/Sentinel","slug":"request-cannot-be-null","errorCode":null,"errorMessage":"Request cannot be null","messagePattern":"Request cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/HttpCommandUtils.java","lineNumber":31,"sourceCode":" * See the License for the specific language governing permissions and\n * limitations under the License.\n */\npackage com.alibaba.csp.sentinel.transport.util;\n\nimport com.alibaba.csp.sentinel.command.CommandRequest;\n\n/**\n * Util class for HTTP command center.\n *\n * @author Eric Zhao\n */\npublic final class HttpCommandUtils {\n\n    public static final String REQUEST_TARGET = \"command-target\";\n\n    public static String getTarget(CommandRequest request) {\n        if (request == null) {\n            throw new IllegalArgumentException(\"Request cannot be null\");\n        }\n        return request.getMetadata().get(REQUEST_TARGET);\n    }\n\n    private HttpCommandUtils() {}\n}\n","sourceCodeStart":13,"sourceCodeEnd":38,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/HttpCommandUtils.java#L13-L38","documentation":"HttpCommandUtils.getTarget extracts the routing target (handler name) from a CommandRequest's metadata. It throws IllegalArgumentException(\"Request cannot be null\") as a programming-error guard when callers pass a null request, since it immediately dereferences request.getMetadata(). This is a fail-fast precondition, not an environmental condition.","triggerScenarios":"HttpCommandUtils.getTarget(null), e.g. custom command-center or handler code where the request variable was never assigned or a conditional parse path returned null.","commonSituations":"Writing a custom transport or command handler that reimplements request dispatch; refactoring that moves request construction behind a conditional; unit tests calling helpers without building a request.","solutions":["Ensure a CommandRequest is always constructed (new CommandRequest()) before calling getTarget","Add an explicit null check at the call site with a meaningful message or skip the call when no request exists","In parsers, fail the connection early instead of propagating null downstream"],"exampleFix":"// before\nCommandRequest req = parseRequest(channel); // may return null on bad input\nString target = HttpCommandUtils.getTarget(req);\n\n// after\nCommandRequest req = parseRequest(channel);\nif (req == null) {\n    writeErrorResponse(400, \"Bad request\", ctx);\n    return;\n}\nString target = HttpCommandUtils.getTarget(req);","handlingStrategy":"type-guard","validationCode":"if (request == null) {\n    throw new IllegalStateException(\"parser produced no CommandRequest\");\n}\nString target = HttpCommandUtils.getTarget(request);","typeGuard":"if (request instanceof CommandRequest cr && cr.getMetadata() != null) { ... }","tryCatchPattern":null,"preventionTips":["Fail the connection early when request parsing fails instead of passing null downstream","Treat a null CommandRequest in dispatch code as a parser bug, not a recoverable state"],"tags":["sentinel","transport","null-check","precondition"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}