Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
DebugUtils in the full utildebug module bootstraps the debug menu: its static initializer registers tool debugs and an activity-lifecycle callback. Its private constructor throws UnsupportedOperationException to prevent instantiation; all public methods (setIconId, addDebugs) are static and rely on the class-level init having run.
Source
Thrown at lib/utildebug/src/main/java/com/blankj/utildebug/DebugUtils.java:31
import com.blankj.utildebug.menu.DebugMenu;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.DrawableRes;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/08/28
* desc : utils about debug
* </pre>
*/
public class DebugUtils {
private DebugUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
static {
List<IDebug> debugList = new ArrayList<>();
AbsToolDebug.addToolDebugs(debugList);
DebugMenu.getInstance().setDebugs(debugList);
getApp().registerActivityLifecycleCallbacks(ActivityLifecycleImpl.instance);
}
public static void setIconId(final @DrawableRes int icon) {
DebugIcon.getInstance().setIconId(icon);
}
public static void addDebugs(final List<IDebug> debugs) {
DebugMenu.getInstance().addDebugs(debugs);
}
public static Application getApp() {View on GitHub (pinned to 7b4caf9e54)
Solutions
- Call the static API: DebugUtils.setIconId(res) / DebugUtils.addDebugs(list) instead of constructing.
- Ensure Utils.init() has run before any DebugUtils touch so its static initializer succeeds.
- Provide a Gson InstanceCreator / Jackson mixin returning a sentinel so the constructor is not invoked.
- Use Mockito.mockStatic for mocking rather than constructing DebugUtils.
Example fix
// before DebugUtils du = new DebugUtils(); // after DebugUtils.setIconId(R.drawable.ic_debug); DebugUtils.addDebugs(debugItems);
Defensive patterns
Strategy: validation
Validate before calling
// Ensure the library is initialized before touching DebugUtils (static initializer needs app).
if (Utils.getApp() == null) {
throw new IllegalStateException("Call Utils.init() before DebugUtils.");
} Prevention
- Call DebugUtils.setIconId(...)/addDebugds(...) statically.
- Ensure Utils.init() completed so the static initializer's getApp() works.
- Register InstanceCreator/mixin for JSON fields.
- Use Mockito.mockStatic for mocking.
When it happens
Trigger: Reflective instantiation via DebugUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan. Instantiating also implies the static initializer (getApp()/DebugMenu setup) ran, which requires the library to be initialized.
Common situations: Serialization frameworks binding a field to DebugUtils; mock frameworks in debug tests; coverage probes; accidental `new DebugUtils()` after weakening modifiers; using DebugUtils before Utils.init() completed so the static initializer's getApp() fails.
Related errors
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/dd8dc14fe4c5329a.
Report an issue: GitHub.