{"record":{"id":"2ee4e00fe38d28ba","repo":"Tencent/tinker","slug":"context-is-null","errorCode":null,"errorMessage":"context is null","messagePattern":"context is null","errorType":"exception","errorClass":"TinkerRuntimeException","httpStatus":null,"severity":"error","filePath":"tinker-android/tinker-android-loader/src/main/java/com/tencent/tinker/loader/shareutil/ShareTinkerInternals.java","lineNumber":740,"sourceCode":"        int i = 0;\n        for (; i < chr.length; i++) {\n            if (chr[i] > 127) {\n                chr[i] = 0;\n                cutFlg = true;\n                break;\n            }\n        }\n\n        if (cutFlg) {\n            return new String(chr, 0, i);\n        } else {\n            return src;\n        }\n    }\n\n    public static void cleanPatch(Context context) {\n        if (context == null) {\n            throw new TinkerRuntimeException(\"context is null\");\n        }\n        final File tinkerDir = SharePatchFileUtil.getPatchDirectory(context);\n        if (!tinkerDir.exists()) {\n            ShareTinkerLog.printErrStackTrace(TAG, new Throwable(),\"try to clean patch while there're not any applied patches.\");\n            return;\n        }\n        final File patchInfoFile = SharePatchFileUtil.getPatchInfoFile(tinkerDir.getAbsolutePath());\n        if (!patchInfoFile.exists()) {\n            ShareTinkerLog.printErrStackTrace(TAG, new Throwable(), \"try to clean patch while patch info file does not exist.\");\n            return;\n        }\n        final File patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(tinkerDir.getAbsolutePath());\n        final SharePatchInfo patchInfo = SharePatchInfo.readAndCheckPropertyWithLock(patchInfoFile, patchInfoLockFile);\n        if (patchInfo != null) {\n            patchInfo.newVersion = \"\";\n            patchInfo.versionToRemove = \"\";\n            SharePatchInfo.rewritePatchInfoFileWithLock(patchInfoFile, patchInfo, patchInfoLockFile);\n        } else {","sourceCodeStart":722,"sourceCodeEnd":758,"githubUrl":"https://github.com/Tencent/tinker/blob/1b7ea02c239840f563ea64fb5bd286eb98d4011e/tinker-android/tinker-android-loader/src/main/java/com/tencent/tinker/loader/shareutil/ShareTinkerInternals.java#L722-L758","documentation":"Thrown by ShareTinkerInternals.cleanPatch(Context) when it is invoked with a null Context. cleanPatch removes the Tinker patch directory (tinker/), the patch info file and lock file, so it needs a real Context to locate app storage. A null Context means the caller (often Tinker.cleanPatch or a service with a destroyed reference) passed an invalid argument.","triggerScenarios":"Calling TinkerInternals.cleanPatch(null) directly, or calling Tinker.cleanPatch / Tinker.cleanPatchWithContext after the retained Context was cleared (e.g., a Service/BroadcastReceiver whose Context member is null, or custom entry code that captures a nullable Context).","commonSituations":"Wiring 'clean patch on crash' or 'rollback' logic in Application subclass or a crash handler where the Context variable is null; calling cleanup from a background process path that has no valid Context; unit tests that pass null.","solutions":["Pass the application Context (getApplicationContext()) instead of a possibly-null field or parameter","Null-check the Context before invoking cleanPatch in your cleanup/crash-handler code","If calling Tinker APIs, prefer Tinker.with(appContext).cleanPatch() which manages Context internally"],"exampleFix":"// before\nShareTinkerInternals.cleanPatch(maybeNullContext);\n\n// after\nContext ctx = maybeNullContext != null ? maybeNullContext : applicationContext;\nif (ctx != null) {\n    ShareTinkerInternals.cleanPatch(ctx);\n}","handlingStrategy":"validation","validationCode":"if (context == null) {\n    Log.w(TAG, \"skip cleanPatch: no context\");\n} else {\n    ShareTinkerInternals.cleanPatch(context);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always derive the Context from getApplicationContext() rather than cached members","Null-check Context fields that outlive activities/services before Tinker API calls"],"tags":["android","null-context","tinker","cleanup"],"backgroundTag":null,"analyzedSha":"1b7ea02c239840f563ea64fb5bd286eb98d4011e","analyzedAt":"2026-08-14T15:16:52.110Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}