JessYanCoding/AndroidAutoSize · error · java.lang.IllegalStateException

you can't instantiate me!

Error message

you can't instantiate me!

What it means

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.

Solutions

  1. Call static methods, e.g. AutoSizeUtils.dp2px(context, value)
  2. Delete the 'new AutoSizeUtils()' call
  3. Pass a Context to the static methods instead of holding an instance

Example fix

// before
AutoSizeUtils utils = new AutoSizeUtils();
int px = utils.dp2px(context, 24f);
// after
int px = AutoSizeUtils.dp2px(context, 24f);
Defensive patterns

Strategy: validation

Validate before calling

// no instance needed; just call statically
int px = AutoSizeUtils.dp2px(context, value);

Type guard

boolean isUtilityClass(Class<?> c) { return Modifier.isPrivate(c.getDeclaredConstructors()[0].getModifiers()); }

Prevention

When it happens

Trigger: Calling new AutoSizeUtils() before using dp2px/px2pt helpers.

Common situations: Novices creating a helper object to call conversion methods; DI or mock frameworks instantiating utility classes.

Related errors


AI-assisted analysis of JessYanCoding/AndroidAutoSize@e402ecdd99 (2026-09-07). Data as JSON: /api/errors/d7274e11b3702ef7. Report an issue: GitHub.

Appendix: source

Thrown at autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java:37

import android.content.Context;
import android.util.TypedValue;
import android.app.Application;

import java.lang.reflect.InvocationTargetException;

/**
 * ================================================
 * AndroidAutoSize 常用工具类
 * <p>
 * Created by JessYan on 2018/8/25 15:24
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class AutoSizeUtils {

    private AutoSizeUtils() {
        throw new IllegalStateException("you can't instantiate me!");
    }

    public static int dp2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int sp2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int pt2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int in2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

View on GitHub (pinned to e402ecdd99)