alibaba/Sentinel · error · IllegalArgumentException

charset can't be null

Error message

charset can't be null

What it means

MetricSearcher reads metric log files line-by-line as text, so it must know the Charset used when they were written (defaultCharset otherwise). A null charset throws IllegalArgumentException because metric decoding would be undefined.

Source

Thrown at sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/metric/MetricSearcher.java:65

     */
    public MetricSearcher(String baseDir, String baseFileName) {
        this(baseDir, baseFileName, defaultCharset);
    }

    /**
     * @param baseDir      metric文件所在目录
     * @param baseFileName metric文件名的关键字,比如 alihot-metrics.log
     * @param charset
     */
    public MetricSearcher(String baseDir, String baseFileName, Charset charset) {
        if (baseDir == null) {
            throw new IllegalArgumentException("baseDir can't be null");
        }
        if (baseFileName == null) {
            throw new IllegalArgumentException("baseFileName can't be null");
        }
        if (charset == null) {
            throw new IllegalArgumentException("charset can't be null");
        }
        this.baseDir = baseDir;
        if (!baseDir.endsWith(File.separator)) {
            this.baseDir += File.separator;
        }
        this.baseFileName = baseFileName;
        metricsReader = new MetricsReader(charset);
    }

    /**
     * 从beginTime开始,检索recommendLines条(大概)记录。同一秒中的数据是原子的,不能分割成多次查询。
     *
     * @param beginTimeMs    检索的最小时间戳
     * @param recommendLines 查询最多想得到的记录条数,返回条数会尽可能不超过这个数字。但是为保证每一秒的数据不被分割,有时候
     *                       返回的记录条数会大于该数字。
     * @return
     * @throws Exception
     */

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Use the two-argument constructor MetricSearcher(baseDir, baseFileName) if you want the default charset.
  2. Otherwise pass a concrete Charset such as Charset.forName("UTF-8") or StandardCharsets.UTF_8.
  3. Fix upstream code that resolves a charset name and can return null.

Example fix

// before
Charset cs = maybeNullCharset();
new MetricSearcher(baseDir, fileName, cs);

// after
new MetricSearcher(baseDir, fileName); // default charset
// or
new MetricSearcher(baseDir, fileName, StandardCharsets.UTF_8);
Defensive patterns

Strategy: validation

Validate before calling

Charset charset = resolveCharset();
if (charset == null) {
    new MetricSearcher(baseDir, baseFileName);      // default charset
} else {
    new MetricSearcher(baseDir, baseFileName, charset);
}

Prevention

When it happens

Trigger: new MetricSearcher(baseDir, baseFileName, null) — the three-arg constructor with a null Charset argument.

Common situations: Injecting a Charset from configuration that failed to resolve (e.g. Charset.forName on a bad name wrapped in a try that yields null), or explicitly passing null intending 'default' instead of using the two-arg constructor.

Related errors


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