{"record":{"id":"f85ff9572c79392d","repo":"alibaba/Sentinel","slug":"invalid-map-instance","errorCode":null,"errorMessage":"Invalid map instance","messagePattern":"Invalid map instance","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java","lineNumber":48,"sourceCode":"\n    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;\n\n    private final ConcurrentLinkedHashMap<T, R> map;\n\n    public ConcurrentLinkedHashMapWrapper(long size) {\n        if (size <= 0) {\n            throw new IllegalArgumentException(\"Cache max capacity should be positive: \" + size);\n        }\n        this.map = new ConcurrentLinkedHashMap.Builder<T, R>()\n            .concurrencyLevel(DEFAULT_CONCURRENCY_LEVEL)\n            .maximumWeightedCapacity(size)\n            .weigher(Weighers.singleton())\n            .build();\n    }\n\n    public ConcurrentLinkedHashMapWrapper(ConcurrentLinkedHashMap<T, R> map) {\n        if (map == null) {\n            throw new IllegalArgumentException(\"Invalid map instance\");\n        }\n        this.map = map;\n    }\n\n    @Override\n    public boolean containsKey(T key) {\n        return map.containsKey(key);\n    }\n\n    @Override\n    public R get(T key) {\n        return map.get(key);\n    }\n\n    @Override\n    public R remove(T key) {\n        return map.remove(key);\n    }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java#L30-L66","documentation":"The second constructor of ConcurrentLinkedHashMapWrapper accepts a pre-built ConcurrentLinkedHashMap instance (used mainly in tests or when you supply your own map) and rejects null with IllegalArgumentException(\"Invalid map instance\"). The wrapper delegates all CacheMap operations to the supplied map, so a null instance would break every subsequent operation.","triggerScenarios":"new ConcurrentLinkedHashMapWrapper<>(null), i.e. calling the delegate-map constructor with a null ConcurrentLinkedHashMap, typically in custom wiring or test setup where the map variable failed to initialize.","commonSituations":"Refactoring code that previously built the map inline into a constructor argument, leaving a null placeholder; conditional initialization where the branch that creates the map is skipped; copy-pasted test scaffolding.","solutions":["Construct the map before wrapping: new ConcurrentLinkedHashMapWrapper<>(new ConcurrentLinkedHashMap.Builder<T,R>().build())","Prefer the size-based constructor new ConcurrentLinkedHashMapWrapper<>(capacity) unless you specifically need a custom map","Add a null check / Objects.requireNonNull at the call site to fail with a clearer message"],"exampleFix":"// before\nConcurrentLinkedHashMap<T,R> map = maybeCreateMap(); // returns null on some path\nwrapper = new ConcurrentLinkedHashMapWrapper<>(map);\n\n// after\nwrapper = new ConcurrentLinkedHashMapWrapper<>(\n    new ConcurrentLinkedHashMap.Builder<T,R>().maximumWeightedCapacity(CAP).build());","handlingStrategy":"type-guard","validationCode":"ConcurrentLinkedHashMap<T,R> m = buildMap();\nif (m == null) throw new IllegalStateException(\"map builder returned null\");\nwrapper = new ConcurrentLinkedHashMapWrapper<>(m);","typeGuard":"private static boolean isWrappable(ConcurrentLinkedHashMap<?,?> m) {\n    return m != null;\n}","tryCatchPattern":null,"preventionTips":["Prefer the size-based constructor unless you truly need a custom map","Initialize the map in the same expression that wraps it"],"tags":["sentinel","cache","null-check","constructor"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}