didi/DoKit · error · IllegalArgumentException
Can't check permissions for null context
Error message
Can't check permissions for null context
What it means
DoKitPermissionUtil.hasPermissions(context, perms...) returns true unconditionally below API 23 (the OS handles install-time permissions), so a null Context can only reach the real check on M+ devices — where it throws IllegalArgumentException('Can't check permissions for null context') before calling ContextCompat.checkSelfPermission. The library treats a null context on M+ as a programming error, since permission state fundamentally cannot be queried without one.
Source
Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/util/DoKitPermissionUtil.java:147
*
* @param context
* @param perms
* @return
*/
public static boolean hasPermissions(@NonNull Context context,
@Size(min = 1) @NonNull String... perms) {
// Always return true for SDK < M, let the system deal with the permissions
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.w(TAG, "hasPermissions: API version < M, returning true by default");
// DANGER ZONE!!! Changing this will break the library.
return true;
}
// Null context may be passed if we have detected Low API (less than M) so getting
// to this point with a null context should not be possible.
if (context == null) {
throw new IllegalArgumentException("Can't check permissions for null context");
}
for (String perm : perms) {
if (ContextCompat.checkSelfPermission(context, perm)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 检测是否有sd卡读写权限,不可靠。无sd卡或者磁盘满了的情况下,即使赋予了权限也可能报无权限
*
* @return
*/
public static boolean checkStorageUnreliable() {View on GitHub (pinned to 626827cddb)
Solutions
- Pass a non-null Context — ideally getApplicationContext() captured at init, or getActivity() guarded for null.
- In fragments, guard: Context ctx = getContext(); if (ctx == null) return; before permission checks.
- Initialize any context field in onCreate/attach before the first hasPermissions call.
- Note the varargs must also be non-empty (@Size(min = 1)); pass at least one permission.
Example fix
// before
if (DoKitPermissionUtil.hasPermissions(null, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { ... }
// after
Context ctx = fragment.getContext();
if (ctx != null && DoKitPermissionUtil.hasPermissions(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { ... } Defensive patterns
Strategy: type-guard
Validate before calling
if (context != null) {
boolean ok = DoKitPermissionUtil.hasPermissions(context, perms);
} else {
// capture a context before permission checks
} Type guard
static boolean canCheckPermissions(Context ctx) {
return ctx != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
} Try / catch
Not recommended — IllegalArgumentException marks a caller bug; supply a real Context instead of catching.
Prevention
- Capture getApplicationContext() at module init and use it for permission checks.
- Guard fragment calls with getContext() null-checks (detached fragments return null).
- Remember the <M path returns true by default, so tests must include API 23+ devices.
When it happens
Trigger: Calling DoKitPermissionUtil.hasPermissions(null, Manifest.permission.X) (or a wrapper that forwards a null Activity/Context) on a device running API 23+; a field-based context used before initialization; a detached fragment whose getActivity() returned null and was passed unchecked.
Common situations: Calling permission helpers from background services or broadcast receivers where a context was never captured; helper classes with a lazily-set application context that is null at first use; testing on old emulators (< M) masking the bug, then crashing on modern devices.
Related errors
- Error image resource invalid.
- Error image already set.
- Error image may not be null.
- Tag invalid.
- Tag already set.
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/83acb285ccebdd4c.
Report an issue: GitHub.