{"record":{"id":"8f191c587c5f7e49","repo":"alibaba/Sentinel","slug":"charset-can-t-be-null-8f191c","errorCode":null,"errorMessage":"charset can't be null","messagePattern":"charset can't be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java","lineNumber":87,"sourceCode":"        this(file, configParser, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET);\n    }\n\n    public FileRefreshableDataSource(File file, Converter<String, T> configParser, Charset charset)\n        throws FileNotFoundException {\n        this(file, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset);\n    }\n\n    public FileRefreshableDataSource(File file, Converter<String, T> configParser, long recommendRefreshMs, int bufSize,\n                                     Charset charset) throws FileNotFoundException {\n        super(configParser, recommendRefreshMs);\n        if (bufSize <= 0 || bufSize > MAX_SIZE) {\n            throw new IllegalArgumentException(\"bufSize must between (0, \" + MAX_SIZE + \"], but \" + bufSize + \" get\");\n        }\n        if (file == null || file.isDirectory()) {\n            throw new IllegalArgumentException(\"File can't be null or a directory\");\n        }\n        if (charset == null) {\n            throw new IllegalArgumentException(\"charset can't be null\");\n        }\n        this.buf = new byte[bufSize];\n        this.file = file;\n        this.charset = charset;\n        // If the file does not exist, the last modified will be 0.\n        this.lastModified = file.lastModified();\n        firstLoad();\n    }\n\n    private void firstLoad() {\n        try {\n            T newValue = loadConfig();\n            getProperty().updateValue(newValue);\n        } catch (Throwable e) {\n            RecordLog.info(\"loadConfig exception\", e);\n        }\n    }\n","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java#L69-L105","documentation":"Thrown by the FileRefreshableDataSource constructor when the Charset argument is null. The charset is used to decode raw file bytes into the String handed to the config parser, so a null charset would cause an NPE on every read; Sentinel rejects it up front instead.","triggerScenarios":"Calling the 3-arg constructor new FileRefreshableDataSource(file, parser, (Charset) null), or the 5-arg constructor with a null charset variable (common when charset is injected/optional and defaults were not applied).","commonSituations":"Passing a charset loaded from config that was never set; refactoring code that previously used a constructor without a charset parameter and accidentally forwarding null; test code that omits the charset argument.","solutions":["Pass an explicit charset, e.g. Charset.forName(\"UTF-8\") or StandardCharsets.UTF_8.","If the charset is optional in your code, default it before calling the constructor: charset = charset == null ? StandardCharsets.UTF_8 : charset.","Use a constructor overload that does not take a charset if you are happy with the library default."],"exampleFix":"// before\nCharset cs = null; // from config, never set\nnew FileRefreshableDataSource<>(file, parser, 3000, 1024 * 1024, cs);\n\n// after\nCharset cs = Optional.ofNullable(configCharset).orElse(StandardCharsets.UTF_8);\nnew FileRefreshableDataSource<>(file, parser, 3000, 1024 * 1024, cs);","handlingStrategy":"validation","validationCode":"Charset cs = (charset == null) ? StandardCharsets.UTF_8 : charset;\nnew FileRefreshableDataSource<>(file, parser, refreshMs, bufSize, cs);","typeGuard":null,"tryCatchPattern":"try {\n    new FileRefreshableDataSource<>(file, parser, cs);\n} catch (IllegalArgumentException e) {\n    throw new IllegalStateException(\"charset must be provided for file data source\", e);\n}","preventionTips":["Default optional charsets to StandardCharsets.UTF_8 in your config layer.","Prefer constructors that omit the charset when the default is acceptable."],"tags":["sentinel","datasource","charset","constructor-validation","java"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}