microg/GmsCore · error · IllegalArgumentException
No context supplied.
Error message
No context supplied.
What it means
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().
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
Example fix
// before
CameraSource.Builder builder = new CameraSource.Builder(context, detector); // context may be null
// after
Context appContext = (context != null) ? context.getApplicationContext() : getApplicationContext();
if (appContext == null) throw new IllegalStateException("Context not ready");
CameraSource.Builder builder = new CameraSource.Builder(appContext, detector); Defensive patterns
Strategy: validation
Validate before calling
if (context == null) throw new IllegalStateException("CameraSource needs a non-null Context"); Type guard
boolean canBuildCameraSource(Context ctx, Detector<?> d) { return ctx != null && d != null; } Try / catch
try {
CameraSource.Builder b = new CameraSource.Builder(context, detector);
} catch (IllegalArgumentException e) {
// context or detector was null; fix initialization
} Prevention
- Use getApplicationContext() for long-lived camera sources
- In Fragments, fetch the context only after onAttach
- Null-check context before any camera setup code
When it happens
Trigger: 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.
Common situations: Using getContext() in a Fragment before onAttach; holding a stale activity reference cleared by a configuration change; unit tests passing null context.
Related errors
- No detector supplied.
- Could not find requested camera.
- Could not find suitable preview frames per second range.
- null camera target
- Callable must not be null
AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06).
Data as JSON: /api/errors/b83778b2c1ad06e3.
Report an issue: GitHub.
Appendix: source
Thrown at play-services-vision-common/src/main/java/com/google/android/gms/vision/CameraSource.java:191
synchronized (this.cameraLock) {
if (camera != null) {
camera.takePicture(shutter::onShutter, null, null, (data, camera) -> jpeg.onPictureTaken(data));
}
}
}
/**
* Builder for configuring and creating an associated camera source.
*/
public static class Builder {
private CameraSource cameraSource = new CameraSource();
/**
* Creates a camera source builder with the supplied context and detector.
* Camera preview images will be streamed to the associated detector upon starting the camera source.
*/
public Builder(Context context, Detector<?> detector) {
if (context == null) throw new IllegalArgumentException("No context supplied.");
if (detector == null) throw new IllegalArgumentException("No detector supplied.");
this.cameraSource.context = context;
this.cameraSource.detector = detector;
}
/**
* Creates an instance of the camera source.
*/
public CameraSource build() {
return cameraSource;
}
/**
* 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).
* Default: false.
*/
public Builder setAutoFocusEnabled(boolean autoFocusEnabled) {
this.cameraSource.autoFocusEnabled = autoFocusEnabled;View on GitHub (pinned to 157c9d86ac)