{"record":{"id":"51bc4fb0b8cef0c3","repo":"alibaba/Sentinel","slug":"config-encoder-cannot-be-null","errorCode":null,"errorMessage":"Config encoder cannot be null","messagePattern":"Config encoder 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":56,"sourceCode":"    private final File file;\n    private final Charset charset;\n\n    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);","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileWritableDataSource.java#L38-L74","documentation":"Thrown by the FileWritableDataSource constructor when the Converter<T, String> config encoder is null. The encoder turns rule entities into the text written to disk, so a null encoder would NPE on the first write(); Sentinel validates it in the constructor instead.","triggerScenarios":"new FileWritableDataSource(file, null) or passing an encoder field that was never initialized (e.g. forgotten @Autowired/bean wiring in Spring).","commonSituations":"Spring bean where the encoder dependency is missing or the @Bean method returns null; refactoring that drops the encoder argument; ordering issue where the data source bean is built before the converter bean.","solutions":["Supply a real encoder, e.g. rules -> JSON.toJSONString(rules) (FastJSON) or an equivalent Jackson/Gson lambda.","In Spring, make the encoder a bean and inject it into the data source bean definition; check startup logs for missing-bean errors.","Add a unit test that constructs the data source with the production wiring to catch null wiring early."],"exampleFix":"// before\nnew FileWritableDataSource<>(file, null);\n\n// after\nnew FileWritableDataSource<>(file, rules -> JSON.toJSONString(rules));","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(encoder, \"config encoder must be provided\");\nnew FileWritableDataSource<>(file, encoder, charset);","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":["Define the encoder as a bean/factory method so it can never be null at wiring time.","Add a smoke test that constructs the data source with production wiring."],"tags":["sentinel","datasource","null-argument","writable-datasource","constructor-validation","java"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}