{"record":{"id":"3f328ff37fe9391e","repo":"alibaba/Sentinel","slug":"charset-cannot-be-null","errorCode":null,"errorMessage":"Charset cannot be null","messagePattern":"Charset cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileWritableDataSource.java","lineNumber":59,"sourceCode":"    private final Lock lock = new ReentrantLock(true);\n\n    public FileWritableDataSource(String filePath, Converter<T, String> configEncoder) {\n        this(new File(filePath), configEncoder);\n    }\n\n    public FileWritableDataSource(File file, Converter<T, String> configEncoder) {\n        this(file, configEncoder, DEFAULT_CHARSET);\n    }\n\n    public FileWritableDataSource(File file, Converter<T, String> configEncoder, Charset charset) {\n        if (file == null || file.isDirectory()) {\n            throw new IllegalArgumentException(\"Bad file\");\n        }\n        if (configEncoder == null) {\n            throw new IllegalArgumentException(\"Config encoder cannot be null\");\n        }\n        if (charset == null) {\n            throw new IllegalArgumentException(\"Charset cannot be null\");\n        }\n        this.configEncoder = configEncoder;\n        this.file = file;\n        this.charset = charset;\n    }\n\n    @Override\n    public void write(T value) throws Exception {\n        lock.lock();\n        try {\n            String convertResult = configEncoder.convert(value);\n            FileOutputStream outputStream = null;\n            try {\n                outputStream = new FileOutputStream(file);\n                byte[] bytesArray = convertResult.getBytes(charset);\n\n                RecordLog.info(\"[FileWritableDataSource] Writing to file {}: {}\", file, convertResult);\n                outputStream.write(bytesArray);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileWritableDataSource.java#L41-L77","documentation":"Thrown by the FileWritableDataSource constructor when the Charset argument is null. The charset controls how the encoded rule text is written to bytes; Sentinel rejects null up front so the failure happens at wiring time, not at first write.","triggerScenarios":"new FileWritableDataSource(file, encoder, null), or forwarding a charset variable from configuration that was never populated.","commonSituations":"Optional charset config key left unset; code migrated from the 2-arg constructor and the third argument accidentally null; test fixtures omitting the charset.","solutions":["Pass StandardCharsets.UTF_8 explicitly.","Default the charset in your own config layer before constructing: charset == null ? StandardCharsets.UTF_8 : charset.","Use the 2-arg constructor if the library default charset is acceptable."],"exampleFix":"// before\nnew FileWritableDataSource<>(file, encoder, null);\n\n// after\nnew FileWritableDataSource<>(file, encoder, StandardCharsets.UTF_8);","handlingStrategy":"validation","validationCode":"Charset cs = (charset == null) ? StandardCharsets.UTF_8 : charset;\nnew FileWritableDataSource<>(file, encoder, cs);","typeGuard":null,"tryCatchPattern":"try {\n    new FileWritableDataSource<>(file, encoder, charset);\n} catch (IllegalArgumentException e) {\n    throw new IllegalStateException(\"FileWritableDataSource misconfigured (encoder/file/charset)\", e);\n}","preventionTips":["Pass StandardCharsets.UTF_8 explicitly rather than relying on injected nullable values."],"tags":["sentinel","datasource","charset","writable-datasource","constructor-validation","java"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}