{"record":{"id":"6fb190d5c02f7176","repo":"karatelabs/karate","slug":"targetid-is-null","errorCode":null,"errorMessage":"targetId is null","messagePattern":"targetId is null","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":3987,"sourceCode":"            String title = (String) target.get(\"title\");\n            String url = (String) target.get(\"url\");\n            if ((title != null && title.contains(titleOrUrl)) ||\n                    (url != null && url.contains(titleOrUrl))) {\n                return (String) target.get(\"targetId\");\n            }\n        }\n        return null;\n    }\n\n    /**\n     * Switch to a page by its backend target ID (unambiguous — avoids URL/title\n     * collisions that {@link #switchPage(String)} is vulnerable to).\n     */\n    @Override\n    public void switchPageById(String targetId) {\n        logger.debug(\"switch page by targetId: {}\", targetId);\n        if (targetId == null) {\n            throw new DriverException(\"targetId is null\");\n        }\n        // Verify the target exists as a page target before activating, retrying within\n        // the auto-wait budget: Target.getTargets can briefly lag a freshly opened tab.\n        int interval = fastPollInterval();\n        int maxAttempts = fastPollAttempts();\n        for (int attempt = 0; ; attempt++) {\n            if (getPages().contains(targetId)) {\n                activateTarget(targetId);\n                return;\n            }\n            if (attempt >= maxAttempts) {\n                break;\n            }\n            sleep(interval);\n        }\n        throw new DriverException(\"no page found with targetId: \" + targetId);\n    }\n","sourceCodeStart":3969,"sourceCodeEnd":4005,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3969-L4005","documentation":"CdpDriver.switchPageById(targetId) rejects a null targetId outright with this DriverException before doing any CDP work. It exists to fail fast on a null handle — typically the result of a previously failed lookup — instead of throwing an obscure NPE deep in CDP messaging.","triggerScenarios":"Passing null to switchPageById, most often because a prior call (findPageTarget, drainOpenedTargets, a stored target id variable) returned null and was forwarded unchecked.","commonSituations":"Chaining switchPageById(resultOfLookup) where the lookup timed out earlier; a Java variable holding the target id never assigned; test data/JSON missing the targetId field.","solutions":["Null-check or assert the targetId before calling switchPageById","Fix the upstream lookup that produced null (usually error 236's 'no page found matching' path)","If id may legitimately be missing, use a guarded switch that skips or reports instead of forwarding null"],"exampleFix":"// before\ndriver.switchPageById(targetIdFromSomewhere); // may be null\n// after\nif (targetIdFromSomewhere != null) {\n    driver.switchPageById(targetIdFromSomewhere);\n} else {\n    throw new IllegalStateException(\"no targetId captured from prior step\");\n}","handlingStrategy":"validation","validationCode":"if (targetId == null || targetId.isBlank()) {\n    throw new IllegalArgumentException(\"switchPageById requires a non-null targetId\");\n}\ndriver.switchPageById(targetId);","typeGuard":null,"tryCatchPattern":"try {\n    driver.switchPageById(targetId);\n} catch (DriverException e) {\n    if (e.getMessage().contains(\"targetId is null\")) {\n        throw new IllegalStateException(\"targetId was never captured from the prior step\", e);\n    }\n    throw e;\n}","preventionTips":["Never forward a lookup result directly to switchPageById without a null check","Capture target ids immediately after opening the tab, and fail early if capture fails","Treat a null id as a prior-step failure, not a driver bug"],"tags":["driver","null-argument","page-switch","target-id"],"backgroundTag":"null-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}