{"record":{"id":"d7274e11b3702ef7","repo":"JessYanCoding/AndroidAutoSize","slug":"you-can-t-instantiate-me-d7274e","errorCode":null,"errorMessage":"you can't instantiate me!","messagePattern":"you can't instantiate me!","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java","lineNumber":37,"sourceCode":"import android.content.Context;\nimport android.util.TypedValue;\nimport android.app.Application;\n\nimport java.lang.reflect.InvocationTargetException;\n\n/**\n * ================================================\n * AndroidAutoSize 常用工具类\n * <p>\n * Created by JessYan on 2018/8/25 15:24\n * <a href=\"mailto:jess.yan.effort@gmail.com\">Contact me</a>\n * <a href=\"https://github.com/JessYanCoding\">Follow me</a>\n * ================================================\n */\npublic class AutoSizeUtils {\n\n    private AutoSizeUtils() {\n        throw new IllegalStateException(\"you can't instantiate me!\");\n    }\n\n    public static int dp2px(Context context, float value) {\n        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n\n    public static int sp2px(Context context, float value) {\n        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n\n    public static int pt2px(Context context, float value) {\n        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n\n    public static int in2px(Context context, float value) {\n        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/JessYanCoding/AndroidAutoSize/blob/e402ecdd998ea07549513ac08c12e9f097f3b48c/autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java#L19-L55","documentation":"This is a deliberate sentinel guard, not an incidental failure. AutoSizeUtils is a static-only utility class exposing dp2px/sp2px/pt2px helpers, so its constructor is private; instantiating it with `new AutoSizeUtils()` (or reflectively) throws this IllegalStateException at once. The faulty input is the instantiation attempt; callers should use the static conversion methods directly.","triggerScenarios":"Calling new AutoSizeUtils() before using dp2px/px2pt helpers.","commonSituations":"Novices creating a helper object to call conversion methods; DI or mock frameworks instantiating utility classes.","solutions":["Call static methods, e.g. AutoSizeUtils.dp2px(context, value)","Delete the 'new AutoSizeUtils()' call","Pass a Context to the static methods instead of holding an instance"],"exampleFix":"// before\nAutoSizeUtils utils = new AutoSizeUtils();\nint px = utils.dp2px(context, 24f);\n// after\nint px = AutoSizeUtils.dp2px(context, 24f);","handlingStrategy":"validation","validationCode":"// no instance needed; just call statically\nint px = AutoSizeUtils.dp2px(context, value);","typeGuard":"boolean isUtilityClass(Class<?> c) { return Modifier.isPrivate(c.getDeclaredConstructors()[0].getModifiers()); }","tryCatchPattern":null,"preventionTips":["Call dp2px/px2dp/pt2px statically with a Context","Never store an AutoSizeUtils instance in a field"],"tags":["java","static-class","units"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"e402ecdd998ea07549513ac08c12e9f097f3b48c","analyzedAt":"2026-09-07T15:05:42.094Z","contentChangedAt":"2026-09-07T15:05:42.094Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}