{"record":{"id":"4fd5524754a23723","repo":"karatelabs/karate","slug":"http-method-expression-evaluated-to-null","errorCode":null,"errorMessage":"http method expression evaluated to null: ","messagePattern":"http method expression evaluated to null: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/StepExecutor.java","lineNumber":2154,"sourceCode":"            Object result = runtime.eval(text.trim());\n            action = result != null ? result.toString() : \"\";\n        }\n        http().header(\"SOAPAction\", action);\n        http().contentType(\"text/xml\");\n        doMethod(\"POST\");\n    }\n\n    private static final Set<String> HTTP_METHODS = Set.of(\n            \"GET\", \"POST\", \"PUT\", \"DELETE\", \"PATCH\", \"HEAD\", \"OPTIONS\", \"CONNECT\", \"TRACE\");\n\n    private void executeMethod(Step step) {\n        String text = step.getText().trim();\n        String method = text.toUpperCase();\n        if (!HTTP_METHODS.contains(method) && !isLiteralMethod(text)) {\n            // e.g. `method httpMethod` where httpMethod is a variable holding 'get'\n            Object result = runtime.eval(text, step);\n            if (result == null) {\n                throw new RuntimeException(\"http method expression evaluated to null: \" + text);\n            }\n            method = result.toString().trim().toUpperCase();\n        }\n        doMethod(method);\n    }\n\n    /**\n     * True when the text after {@code method} should be taken as the verb itself instead of\n     * being evaluated. A bare word that is not a variable is a custom verb such as PURGE or\n     * PROPFIND - anything else (a variable name, a quoted string, a function call) is an\n     * expression that resolves to the verb.\n     */\n    private boolean isLiteralMethod(String text) {\n        for (int i = 0; i < text.length(); i++) {\n            if (!Character.isLetter(text.charAt(i))) {\n                return false;\n            }\n        }","sourceCodeStart":2136,"sourceCodeEnd":2172,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/StepExecutor.java#L2136-L2172","documentation":"For a 'method' step whose text is not a literal HTTP verb, Karate evaluates the text as an expression expecting a string like 'get' or 'post'. If the expression evaluates to null, Karate throws this RuntimeException because there is no HTTP method to execute.","triggerScenarios":"Using 'method httpMethod' (or 'method config.method') where the referenced variable is undefined or evaluates to null at runtime — e.g. the variable was never 'def'ined, or a JSON path resolved to nothing.","commonSituations":"Typos in variable names; variables expected to be set by a called feature or data-driven setup that didn't run; reading a method name from a config/JSON key that is absent; case where the value is null after a failed match.","solutions":["Define the variable before the method step: 'def httpMethod = \"get\"'","Print the variable right before the step to confirm it is non-null","Check the spelling of the variable and that the source feature/JSON actually sets it","Add a guard assertion earlier: 'assert httpMethod != null' or use karate's match to validate setup"],"exampleFix":"// before: variable undefined -> null -> error\nWhen method httpVerb\n// after\ndef httpVerb = 'post'\nWhen method httpVerb","handlingStrategy":"validation","validationCode":"// ensure the method variable resolves before the method step\nObject m = karate.get('httpMethod');\nif (m == null) throw new IllegalStateException(\"httpMethod variable must be defined before 'method' step\");","typeGuard":"static boolean hasMethodVar(io.karatelabs.core.ScenarioEngine k) { return k.get(\"httpMethod\") != null; }","tryCatchPattern":"try { executor.execute(methodStep); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"http method expression evaluated to null\")) { throw new IllegalStateException(\"define the method variable: \" + e.getMessage()); } throw e; }","preventionTips":["def the method variable before the method step","print the variable to debug null setup","Verify called setup features actually assign it","Use literal verbs ('When method get') when no dynamic choice is needed"],"tags":["karate","http","method","null"],"backgroundTag":"null-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}