{"record":{"id":"21888cfeeefa347f","repo":"Tencent/matrix","slug":"context-should-not-be-null","errorCode":null,"errorMessage":"Context should not be null","messagePattern":"Context should not be null","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/BatteryEventDelegate.java","lineNumber":101,"sourceCode":"        }\n        return sInstance;\n    }\n\n    final Context mContext;\n    final List<Listener> mListenerList = new LinkedList<>();\n    final Handler mUiHandler = new Handler(Looper.getMainLooper());\n    final BackgroundTask mAppLowEnergyTask = new BackgroundTask();\n    boolean sIsForeground = true;\n    long mLastBatteryPowerPct;\n    long mLastBatteryTemp;\n\n    @Nullable\n    BatteryMonitorCore mCore;\n\n\n    BatteryEventDelegate(Context context) {\n        if (context == null) {\n            throw new IllegalStateException(\"Context should not be null\");\n        }\n        mContext = context;\n        mLastBatteryPowerPct = BatteryCanaryUtil.getBatteryPercentageImmediately(context);\n        mLastBatteryTemp = BatteryCanaryUtil.getBatteryTemperature(context);\n    }\n\n    public BatteryEventDelegate attach(BatteryMonitorCore core) {\n        if (core != null) {\n            mCore = core;\n        }\n        return this;\n    }\n\n    @RestrictTo(RestrictTo.Scope.LIBRARY)\n    public void onForeground(boolean isForeground) {\n        sIsForeground = isForeground;\n        if (isForeground) {\n            sBackgroundBgnMillis = 0L;","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/BatteryEventDelegate.java#L83-L119","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass a valid non-null context: use getApplicationContext() from Application rather than a nullable field.","Null-check the context source before calling init().","In tests, supply a Mockito/Robolectric Application context instead of null."],"exampleFix":"// before\nBatteryEventDelegate.init(nullableContext); // may be null\n\n// after\nContext app = (context != null) ? context.getApplicationContext() : null;\nif (app == null) {\n    throw new IllegalArgumentException(\"init requires an application context\");\n}\nBatteryEventDelegate.init(app);","handlingStrategy":"validation","validationCode":"Context app = context != null ? context.getApplicationContext() : null;\nif (app == null) return; // skip init instead of passing null\nBatteryEventDelegate.init(app);","typeGuard":"// Java: explicit null check\nstatic boolean hasContext(Context c) { return c != null; }","tryCatchPattern":"try {\n    BatteryEventDelegate.init(ctx);\n} catch (IllegalStateException e) {\n    MatrixLog.e(TAG, \"battery delegate init skipped: null context\");\n}","preventionTips":["Always pass getApplicationContext(), never a destroyed Activity reference.","Null-check context sources in early-lifecycle code before init.","Use Robolectric/Mockito Application in tests rather than null."],"tags":["android","null-check","context","battery"],"backgroundTag":"null-argument","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}