Tencent/matrix · error · IllegalStateException

Context should not be null

Error message

Context should not be null

What it means

The package-private BatteryEventDelegate constructor throws IllegalStateException when the passed Context is null. A null context would make subsequent immediate battery-percentage/temperature lookups (BatteryCanaryUtil.getBatteryPercentageImmediately(context)) impossible, so construction fails fast.

Solutions

  1. Pass a valid non-null context: use getApplicationContext() from Application rather than a nullable field.
  2. Null-check the context source before calling init().
  3. In tests, supply a Mockito/Robolectric Application context instead of null.

Example fix

// before
BatteryEventDelegate.init(nullableContext); // may be null

// after
Context app = (context != null) ? context.getApplicationContext() : null;
if (app == null) {
    throw new IllegalArgumentException("init requires an application context");
}
BatteryEventDelegate.init(app);
Defensive patterns

Strategy: validation

Validate before calling

Context app = context != null ? context.getApplicationContext() : null;
if (app == null) return; // skip init instead of passing null
BatteryEventDelegate.init(app);

Type guard

// Java: explicit null check
static boolean hasContext(Context c) { return c != null; }

Try / catch

try {
    BatteryEventDelegate.init(ctx);
} catch (IllegalStateException e) {
    MatrixLog.e(TAG, "battery delegate init skipped: null context");
}

Prevention

When it happens

Trigger: Calling BatteryEventDelegate.init(null) or otherwise passing a null Context into the constructor — typically when getApplicationContext() or a lazily-held Application reference returns null (e.g. in tests or very early app lifetime).

Common situations: Passing an Activity that has already been destroyed and cleared; calling init() in a unit test with a null mock; Application reference not yet attached in a ContentProvider's early callback.

Related errors


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

Appendix: source

Thrown at matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/BatteryEventDelegate.java:101

        }
        return sInstance;
    }

    final Context mContext;
    final List<Listener> mListenerList = new LinkedList<>();
    final Handler mUiHandler = new Handler(Looper.getMainLooper());
    final BackgroundTask mAppLowEnergyTask = new BackgroundTask();
    boolean sIsForeground = true;
    long mLastBatteryPowerPct;
    long mLastBatteryTemp;

    @Nullable
    BatteryMonitorCore mCore;


    BatteryEventDelegate(Context context) {
        if (context == null) {
            throw new IllegalStateException("Context should not be null");
        }
        mContext = context;
        mLastBatteryPowerPct = BatteryCanaryUtil.getBatteryPercentageImmediately(context);
        mLastBatteryTemp = BatteryCanaryUtil.getBatteryTemperature(context);
    }

    public BatteryEventDelegate attach(BatteryMonitorCore core) {
        if (core != null) {
            mCore = core;
        }
        return this;
    }

    @RestrictTo(RestrictTo.Scope.LIBRARY)
    public void onForeground(boolean isForeground) {
        sIsForeground = isForeground;
        if (isForeground) {
            sBackgroundBgnMillis = 0L;

View on GitHub (pinned to 3b8293bd65)