{"record":{"id":"a17b5c2cc9e06c33","repo":"pockethub/PocketHub","slug":"activity-cannot-be-null","errorCode":null,"errorMessage":"Activity cannot be null","messagePattern":"Activity cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/com/github/pockethub/android/accounts/AccountUtils.java","lineNumber":232,"sourceCode":"\n    /**\n     * Get account used for authentication\n     *\n     * @param manager\n     * @param activity\n     * @return account\n     * @throws IOException\n     * @throws AccountsException\n     */\n    public static Account getAccount(final AccountManager manager,\n            final Activity activity) throws IOException, AccountsException {\n        final boolean loggable = Log.isLoggable(TAG, DEBUG);\n        if (loggable) {\n            Log.d(TAG, \"Getting account\");\n        }\n\n        if (activity == null) {\n            throw new IllegalArgumentException(\"Activity cannot be null\");\n        }\n\n        if (activity.isFinishing()) {\n            throw new OperationCanceledException();\n        }\n\n        Account[] accounts;\n        try {\n            if (!hasAuthenticator(manager)) {\n                throw new AuthenticatorConflictException();\n            }\n\n            while ((accounts = getAccounts(manager)).length == 0) {\n                if (loggable) {\n                    Log.d(TAG, \"No GitHub accounts for activity=\" + activity);\n                }\n\n                Bundle result = manager.addAccount(ACCOUNT_TYPE, null, null,","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/pockethub/PocketHub/blob/8228cb8f7197a7c2227e67ee48a2c8dc843fb553/app/src/main/java/com/github/pockethub/android/accounts/AccountUtils.java#L214-L250","documentation":"AccountUtils.getAccount() requires a non-null Activity to resolve accounts and show account selection UI. It defensively validates its argument and throws IllegalArgumentException when the caller passes null. This is a programmer-error guard, not a runtime condition.","triggerScenarios":"Calling AccountUtils.getAccount(context-as-Activity, ...) with a null Activity — typically passing getContext() from a Fragment before it is attached, or storing an Activity reference after onDestroy().","commonSituations":"See trigger scenarios.","solutions":["Ensure a valid, attached Activity is available before calling getAccount — check isAdded() in a Fragment.","Pass requireActivity() (Fragment) instead of getContext() when an Activity is required.","Do not cache the Activity in a long-lived field; re-fetch it at call time.","If the call can happen post-detach, skip it instead of calling with null."],"exampleFix":"// before\nAccount account = AccountUtils.getAccount(getContext(), account, ...);\n// after\nif (getActivity() != null && isAdded()) {\n    Account account = AccountUtils.getAccount(getActivity(), account, ...);\n}","handlingStrategy":"validation","validationCode":"if (activity == null || activity.isFinishing()) {\n    return; // skip account lookup\n}","typeGuard":"fun Fragment.activityOrNull(): Activity? = if (isAdded) activity else null","tryCatchPattern":null,"preventionTips":["Use requireActivity()/isAdded checks in Fragments before context-dependent calls","Never store Activity references in long-lived objects","Validate non-null Activity in a wrapper around AccountUtils","Move account work to a ViewModel/Application scope where possible"],"tags":["android","null-check","activity-lifecycle","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"8228cb8f7197a7c2227e67ee48a2c8dc843fb553","analyzedAt":"2026-09-11T10:16:17.606Z","contentChangedAt":"2026-09-11T10:16:17.606Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}