Tencent/matrix · error · NullPointerException

reporter == null

Error message

reporter == null

What it means

MatrixCloseGuard.setReporter() replaces the Reporter used to warn about CloseGuard violations (unclosed resources). The API contract requires a non-null reporter, so passing null throws NullPointerException("reporter == null"). This mirrors the framework CloseGuard behavior Matrix re-implements for IO leak detection.

Solutions

  1. Never pass null — supply a real Reporter implementation (e.g. the IoCanary DefaultReporter) or simply stop calling setReporter().
  2. If you want to stop reporting, call the appropriate Matrix disable/close API instead of setting a null reporter.
  3. Null-check or lazily create the reporter before invoking setReporter().
  4. If the reporter comes from DI/configuration, assert it is initialized during app startup.

Example fix

// before
MatrixCloseGuard.setReporter(null); // teardown attempt
// after
MatrixCloseGuard.setReporter(new MatrixCloseGuard.Reporter() {
    @Override public void closeLeak(String where, String why) { /* log or report */ }
});
Defensive patterns

Strategy: type-guard

Validate before calling

if (reporter != null) {
    MatrixCloseGuard.setReporter(reporter);
}

Type guard

boolean isValidReporter(MatrixCloseGuard.Reporter r) {
    return r != null;
}

Try / catch

try {
    MatrixCloseGuard.setReporter(reporter);
} catch (NullPointerException e) {
    MatrixLog.e(TAG, "setReporter called with null; keeping previous reporter");
}

Prevention

When it happens

Trigger: Calling MatrixCloseGuard.setReporter(null) — typically during teardown/reset of IoCanary, or when the reporter is stored in a field that was never initialized before being passed in.

Common situations: An app lifecycle component clearing listeners by setting the reporter to null instead of using a proper disable/uninstall API; a DI-furnished reporter that is null in a debug/early-init build variant; refactoring that renamed the reporter field but left a null default.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/30a9839c20395c0a. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-io-canary/src/main/java/com/tencent/matrix/iocanary/detect/MatrixCloseGuard.java:152

        }
        return new MatrixCloseGuard();
    }

    /**
     * Used to enable or disable CloseGuard. Note that CloseGuard only
     * warns if it is enabled for both allocation and finalization.
     */
    public static void setEnabled(boolean enabled) {
        sENABLED = enabled;
    }

    /**
     * Used to replace default Reporter used to warn of CloseGuard
     * violations. Must be non-null.
     */
    public static void setReporter(Reporter reporter) {
        if (reporter == null) {
            throw new NullPointerException("reporter == null");
        }
        sREPORTER = reporter;
    }

    /**
     * Returns non-null CloseGuard.Reporter.
     */
    public static Reporter getReporter() {
        return sREPORTER;
    }

    private MatrixCloseGuard() {
    }

    /**
     * If CloseGuard is enabled, {@code open} initializes the instance
     * with a warning that the caller should have explicitly called the
     * {@code closer} method instead of relying on finalization.

View on GitHub (pinned to 3b8293bd65)