Blankj/AndroidUtilCode · critical · NullPointerException
reflect failed.
Error message
reflect failed.
What it means
Utils.getApp() returns the cached Application (sApp); if it is null, it attempts to recover via UtilsBridge.getApplicationByReflect() and re-init. If after that sApp is still null, it throws NullPointerException("reflect failed."). This means the library was never initialized AND reflection could not locate the Application — a fatal, unrecoverable library state.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java:72
return;
}
if (sApp.equals(app)) return;
UtilsBridge.unInit(sApp);
sApp = app;
UtilsBridge.init(sApp);
}
/**
* Return the Application object.
* <p>Main process get app by UtilsFileProvider,
* and other process get app by reflect.</p>
*
* @return the Application object
*/
public static Application getApp() {
if (sApp != null) return sApp;
init(UtilsBridge.getApplicationByReflect());
if (sApp == null) throw new NullPointerException("reflect failed.");
Log.i("Utils", UtilsBridge.getCurrentProcessName() + " reflect app success.");
return sApp;
}
///////////////////////////////////////////////////////////////////////////
// interface
///////////////////////////////////////////////////////////////////////////
public abstract static class Task<Result> extends ThreadUtils.SimpleTask<Result> {
private Consumer<Result> mConsumer;
public Task(final Consumer<Result> consumer) {
mConsumer = consumer;
}
@Override
public void onSuccess(Result result) {View on GitHub (pinned to 7b4caf9e54)
Solutions
- Call Utils.init(application) explicitly in your Application.onCreate() before using any util.
- For unit tests on the JVM, use Robolectric or mock Utils.getApp() rather than relying on reflection.
- Keep the library's UtilsFileProvider registered in the manifest so init happens automatically on cold start, or ensure your ContentProvider init does not race ahead of Application creation.
- Add proguard/R8 keep rules for the Application class and the ActivityThread reflection path so getApplicationByReflect() succeeds in release builds.
Example fix
// before
// Application.onCreate does not init the library
int w = ScreenUtils.getScreenWidth(); // -> Utils.getApp() -> reflect failed.
// after
public class App extends Application {
@Override public void onCreate() {
super.onCreate();
Utils.init(this);
}
} Defensive patterns
Strategy: validation
Validate before calling
// Guard any code path that will call Utils.getApp().
public static Application requireApp() {
Application app = Utils.getApp(); // will throw if reflect fails
return app;
}
// Better: ensure init happened before first use.
if (Utils.getApp() == null) {
// fallback only if you intentionally control init timing
} Try / catch
try {
Application app = Utils.getApp();
} catch (NullPointerException e) {
// reflect failed: report init/r8/manifest problem; do NOT silently continue
throw new IllegalStateException("AndroidUtilCode not initialized", e);
} Prevention
- Always call Utils.init(this) in Application.onCreate before using any util.
- Register UtilsFileProvider in the manifest for auto-init on cold start.
- Add ProGuard/R8 keep rules for the Application class and ActivityThread reflection path.
- In unit tests, use Robolectric or mock Utils.getApp(); never rely on reflection on a bare JVM.
- Do not use util classes inside ContentProviders that run before Application.onCreate without ensuring init.
When it happens
Trigger: Calling any util that internally calls Utils.getApp() (e.g. ScreenUtils.getScreenWidth) before Utils.init() was invoked, in a context where reflection cannot find the Application (e.g. unit tests running on the JVM without an Android runtime, multi-process setups without UtilsFileProvider, or proguard/R8 stripping the Application class metadata).
Common situations: Running pure JUnit unit tests (no Robolectric/Android manifest) so reflection finds no ActivityThread.currentApplication(); forgetting Utils.init(this) in Application.onCreate; obfuscation stripping the ActivityThread access path; instantiating utils in a ContentProvider that runs before Application.onCreate on cold start.
Related errors
- u should init first
- u can't instantiate me...
- u can't instantiate me...
- ContentView is null.
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/d3e3f90a6a087979.
Report an issue: GitHub.