alibaba/Sentinel · error · IllegalArgumentException

Charset cannot be null

Error message

Charset cannot be null

What it means

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.

Source

Thrown at sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileWritableDataSource.java:59

    private final Lock lock = new ReentrantLock(true);

    public FileWritableDataSource(String filePath, Converter<T, String> configEncoder) {
        this(new File(filePath), configEncoder);
    }

    public FileWritableDataSource(File file, Converter<T, String> configEncoder) {
        this(file, configEncoder, DEFAULT_CHARSET);
    }

    public FileWritableDataSource(File file, Converter<T, String> configEncoder, Charset charset) {
        if (file == null || file.isDirectory()) {
            throw new IllegalArgumentException("Bad file");
        }
        if (configEncoder == null) {
            throw new IllegalArgumentException("Config encoder cannot be null");
        }
        if (charset == null) {
            throw new IllegalArgumentException("Charset cannot be null");
        }
        this.configEncoder = configEncoder;
        this.file = file;
        this.charset = charset;
    }

    @Override
    public void write(T value) throws Exception {
        lock.lock();
        try {
            String convertResult = configEncoder.convert(value);
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                byte[] bytesArray = convertResult.getBytes(charset);

                RecordLog.info("[FileWritableDataSource] Writing to file {}: {}", file, convertResult);
                outputStream.write(bytesArray);

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Pass StandardCharsets.UTF_8 explicitly.
  2. Default the charset in your own config layer before constructing: charset == null ? StandardCharsets.UTF_8 : charset.
  3. Use the 2-arg constructor if the library default charset is acceptable.

Example fix

// before
new FileWritableDataSource<>(file, encoder, null);

// after
new FileWritableDataSource<>(file, encoder, StandardCharsets.UTF_8);
Defensive patterns

Strategy: validation

Validate before calling

Charset cs = (charset == null) ? StandardCharsets.UTF_8 : charset;
new FileWritableDataSource<>(file, encoder, cs);

Try / catch

try {
    new FileWritableDataSource<>(file, encoder, charset);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("FileWritableDataSource misconfigured (encoder/file/charset)", e);
}

Prevention

When it happens

Trigger: new FileWritableDataSource(file, encoder, null), or forwarding a charset variable from configuration that was never populated.

Common situations: Optional charset config key left unset; code migrated from the 2-arg constructor and the third argument accidentally null; test fixtures omitting the charset.

Related errors


AI-assisted analysis of alibaba/Sentinel@a3f40ba8e9 (2026-08-14). Data as JSON: /api/errors/3f328ff37fe9391e. Report an issue: GitHub.