{"record":{"id":"3a22381eec373eba","repo":"Tencent/tinker","slug":"context-must-not-be-null-3a2238","errorCode":null,"errorMessage":"Context must not be null.","messagePattern":"Context must not be null\\.","errorType":"exception","errorClass":"TinkerRuntimeException","httpStatus":null,"severity":"error","filePath":"tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/tinker/Tinker.java","lineNumber":350,"sourceCode":"        private final boolean mainProcess;\n        private final boolean patchProcess;\n\n        private int status = -1;\n        private LoadReporter  loadReporter;\n        private PatchReporter patchReporter;\n        private PatchListener listener;\n        private AbstractFilePatch patcher;\n        private File          patchDirectory;\n        private File          patchInfoFile;\n        private File          patchInfoLockFile;\n        private Boolean       tinkerLoadVerifyFlag;\n\n        /**\n         * Start building a new {@link Tinker} instance.\n         */\n        public Builder(Context context) {\n            if (context == null) {\n                throw new TinkerRuntimeException(\"Context must not be null.\");\n            }\n            this.context = context;\n            this.mainProcess = TinkerServiceInternals.isInMainProcess(context);\n            this.patchProcess = TinkerServiceInternals.isInTinkerPatchServiceProcess(context);\n            this.patchDirectory = SharePatchFileUtil.getPatchDirectory(context);\n            if (this.patchDirectory == null) {\n                ShareTinkerLog.e(TAG, \"patchDirectory is null!\");\n                return;\n            }\n            this.patchInfoFile = SharePatchFileUtil.getPatchInfoFile(patchDirectory.getAbsolutePath());\n            this.patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(patchDirectory.getAbsolutePath());\n            ShareTinkerLog.w(TAG, \"tinker patch directory: %s\", patchDirectory);\n        }\n\n        public Builder tinkerFlags(int tinkerFlags) {\n            if (this.status != -1) {\n                throw new TinkerRuntimeException(\"tinkerFlag is already set.\");\n            }","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/Tencent/tinker/blob/1b7ea02c239840f563ea64fb5bd286eb98d4011e/tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/tinker/Tinker.java#L332-L368","documentation":"Tinker.Builder's constructor immediately rejects a null Context. The builder needs a Context to derive process identity (main vs :patch), the patch directory, and info files, so a null context makes the whole configuration meaningless and fails fast.","triggerScenarios":"Constructing new Tinker.Builder(null) — usually because a Context field was not yet assigned (constructor ordering), a DI-provided context was null, or a test passed null.","commonSituations":"Custom ApplicationLike subclasses that build a Tinker in onBaseContextAttached before attachBaseContext finishes; unit tests without Robolectric; refactor moving builder creation before context initialization.","solutions":["Pass applicationContext, and construct the Builder only after the context is attached.","In ApplicationLike, create the builder in onCreate (not earlier lifecycle hooks) when in doubt.","Add a null assertion upstream so the failure points at the real missing dependency."],"exampleFix":"// before\npublic MyAppLike(Application app) {\n    this.builder = new Tinker.Builder(this.context); // context not yet set -> null\n}\n\n// after\n@Override public void onCreate() {\n    this.builder = new Tinker.Builder(getApplication().getApplicationContext());\n}","handlingStrategy":"validation","validationCode":"if (context == null) throw new IllegalArgumentException(\"context required before tinker init\");\nTinker.Builder builder = new Tinker.Builder(context.getApplicationContext());","typeGuard":"public static boolean isValidContext(Context c) {\n    return c != null && c.getApplicationContext() != null;\n}","tryCatchPattern":null,"preventionTips":["Build the Tinker.Builder in ApplicationLike.onCreate when the context is guaranteed attached.","Always pass getApplicationContext(), never a nullable field.","In tests, use Robolectric to supply a real Context."],"tags":["tinker","android","builder","context","null-check"],"backgroundTag":null,"analyzedSha":"1b7ea02c239840f563ea64fb5bd286eb98d4011e","analyzedAt":"2026-08-14T15:16:52.110Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}