{"record":{"id":"f95ced04e660e9fb","repo":"alibaba/spring-cloud-alibaba","slug":"file-and-charset-must-not-be-null","errorCode":null,"errorMessage":"file and charset must not be null","messagePattern":"file and charset must not be null","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java","lineNumber":50,"sourceCode":" * @see FileRefreshableDataSource\n */\npublic class FileRefreshableDataSourceFactoryBean\n\t\timplements FactoryBean<FileRefreshableDataSource> {\n\n\tprivate @Nullable String file;\n\n\tprivate @Nullable String charset;\n\n\tprivate long recommendRefreshMs;\n\n\tprivate int bufSize;\n\n\tprivate @Nullable Converter converter;\n\n\t@Override\n\tpublic FileRefreshableDataSource getObject() throws Exception {\n\t\tif (file == null || charset == null) {\n\t\t\tthrow new IllegalStateException(\"file and charset must not be null\");\n\t\t}\n\t\treturn new FileRefreshableDataSource(new File(file), converter,\n\t\t\t\trecommendRefreshMs, bufSize, Charset.forName(charset));\n\t}\n\n\t@Override\n\tpublic Class<?> getObjectType() {\n\t\treturn FileRefreshableDataSource.class;\n\t}\n\n\tpublic @Nullable String getFile() {\n\t\treturn file;\n\t}\n\n\tpublic void setFile(@Nullable String file) {\n\t\tthis.file = file;\n\t}\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/alibaba/spring-cloud-alibaba/blob/115d5901102009492e05d5ec18c3f79cad4077d0/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java#L32-L68","documentation":"Thrown by FileRefreshableDataSourceFactoryBean.getObject() when either file or charset is null. This FactoryBean creates a FileRefreshableDataSource that polls a file for Sentinel rule updates. Both file and charset are @Nullable with no default values (note: FileDataSourceProperties has charset defaulting to 'utf-8', but the FactoryBean field itself does not — if wired directly without going through the properties preCheck, the charset can be null). The check fires during Spring bean initialization.","triggerScenarios":"Creating a FileRefreshableDataSourceFactoryBean (directly or via Sentinel datasource config) without setting the file path or charset property. When wired through the standard Sentinel auto-configuration, FileDataSourceProperties.preCheck should have caught a null file earlier, but charset could still be null if the FactoryBean is constructed programmatically or if the properties-to-bean binding skips charset.","commonSituations":"1) Programmatic FactoryBean creation without calling setFile() or setCharset(). 2) Configuration binding issue where the charset property is not mapped. 3) Custom FactoryBean instantiation in test code that omits required setters.","solutions":["Ensure both file and charset are set: file should be a valid filesystem path, charset should be a valid charset name (e.g., 'UTF-8').","When using spring.cloud.sentinel.datasource config, verify spring.cloud.sentinel.datasource.<name>.file.file and spring.cloud.sentinel.datasource.<name>.file.charset are both set.","If constructing the FactoryBean programmatically, call setFile(path) and setCharset('UTF-8') before getObject()."],"exampleFix":"// before (programmatic, missing charset)\nFileRefreshableDataSourceFactoryBean fb = new FileRefreshableDataSourceFactoryBean();\nfb.setFile(\"/opt/app/sentinel-rules.json\");\nfb.setConverter(converter);\nfb.getObject(); // throws\n\n// after (fixed)\nFileRefreshableDataSourceFactoryBean fb = new FileRefreshableDataSourceFactoryBean();\nfb.setFile(\"/opt/app/sentinel-rules.json\");\nfb.setCharset(\"UTF-8\");\nfb.setConverter(converter);\nfb.getObject();","handlingStrategy":"validation","validationCode":"// Validate FileRefreshableDataSource FactoryBean properties\nFileRefreshableDataSourceFactoryBean fb = new FileRefreshableDataSourceFactoryBean();\nif (file == null || charset == null) {\n    throw new IllegalStateException(\n        \"FileRefreshableDataSource requires both 'file' and 'charset'\");\n}\nfb.setFile(file);\nfb.setCharset(charset != null ? charset : \"UTF-8\");\nfb.setConverter(converter);\nfb.setRecommendRefreshMs(3000L);\nfb.setBufSize(1024 * 1024);\nfb.getObject();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set charset explicitly — default to 'UTF-8' if unsure.","When configuring via spring.cloud.sentinel.datasource, verify both file.file and file.charset are set.","Use @ConfigurationProperties validation to enforce non-null file and charset at binding time."],"tags":["sentinel","file-datasource","factory-bean","configuration","validation"],"backgroundTag":null,"analyzedSha":"115d5901102009492e05d5ec18c3f79cad4077d0","analyzedAt":"2026-08-14T04:47:13.900Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}