{"record":{"id":"b83778b2c1ad06e3","repo":"microg/GmsCore","slug":"no-context-supplied","errorCode":null,"errorMessage":"No context supplied.","messagePattern":"No context supplied\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"play-services-vision-common/src/main/java/com/google/android/gms/vision/CameraSource.java","lineNumber":191,"sourceCode":"        synchronized (this.cameraLock) {\n            if (camera != null) {\n                camera.takePicture(shutter::onShutter, null, null, (data, camera) -> jpeg.onPictureTaken(data));\n            }\n        }\n    }\n\n    /**\n     * Builder for configuring and creating an associated camera source.\n     */\n    public static class Builder {\n        private CameraSource cameraSource = new CameraSource();\n\n        /**\n         * Creates a camera source builder with the supplied context and detector.\n         * Camera preview images will be streamed to the associated detector upon starting the camera source.\n         */\n        public Builder(Context context, Detector<?> detector) {\n            if (context == null) throw new IllegalArgumentException(\"No context supplied.\");\n            if (detector == null) throw new IllegalArgumentException(\"No detector supplied.\");\n            this.cameraSource.context = context;\n            this.cameraSource.detector = detector;\n        }\n\n        /**\n         * Creates an instance of the camera source.\n         */\n        public CameraSource build() {\n            return cameraSource;\n        }\n\n        /**\n         * Sets whether to enable camera auto focus. If set to false (default), the camera's default focus setting is used. If set to true, a continuous video focus setting is used (if supported by the camera hardware).\n         * Default: false.\n         */\n        public Builder setAutoFocusEnabled(boolean autoFocusEnabled) {\n            this.cameraSource.autoFocusEnabled = autoFocusEnabled;","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-vision-common/src/main/java/com/google/android/gms/vision/CameraSource.java#L173-L209","documentation":"CameraSource.Builder's constructor throws IllegalArgumentException when the supplied Context is null. A Context is mandatory for the camera source to access camera hardware and application resources, so the library fails fast at build time rather than later at start().","triggerScenarios":"Calling new CameraSource.Builder(null, detector) — typically because an Activity/Context field was not yet initialized, was captured before onCreate, or a mocked/null context was injected in a test.","commonSituations":"Using getContext() in a Fragment before onAttach; holding a stale activity reference cleared by a configuration change; unit tests passing null context.","solutions":["Pass a valid non-null Context, e.g. activity.getApplicationContext()","In a Fragment, obtain the context after onAttach/onCreateView rather than in field initializers","Guard the builder call with a null check and fail early with your own message","In tests, use an InstrumentationRegistry or Robolectric context instead of null"],"exampleFix":"// before\nCameraSource.Builder builder = new CameraSource.Builder(context, detector); // context may be null\n// after\nContext appContext = (context != null) ? context.getApplicationContext() : getApplicationContext();\nif (appContext == null) throw new IllegalStateException(\"Context not ready\");\nCameraSource.Builder builder = new CameraSource.Builder(appContext, detector);","handlingStrategy":"validation","validationCode":"if (context == null) throw new IllegalStateException(\"CameraSource needs a non-null Context\");","typeGuard":"boolean canBuildCameraSource(Context ctx, Detector<?> d) { return ctx != null && d != null; }","tryCatchPattern":"try {\n    CameraSource.Builder b = new CameraSource.Builder(context, detector);\n} catch (IllegalArgumentException e) {\n    // context or detector was null; fix initialization\n}","preventionTips":["Use getApplicationContext() for long-lived camera sources","In Fragments, fetch the context only after onAttach","Null-check context before any camera setup code"],"tags":["vision","camera","null-check","java"],"backgroundTag":"null-argument","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}