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 a normal runtime error. Preconditions is a final, static-only assertion helper (checkMainThread, etc.), so its constructor is private; any `new Preconditions()` call (including via reflection) throws this IllegalStateException immediately. The input at fault is the instantiation attempt itself; correct usage is to invoke its static check methods.

Solutions

  1. Call static methods like Preconditions.checkNotNull(value) directly
  2. Remove the instantiation
  3. Exclude the class from auto-instantiation tooling

Example fix

// before
Preconditions p = new Preconditions();
// after
Preconditions.checkNotNull(value, "value must not be null");
Defensive patterns

Strategy: try-catch

Validate before calling

if (Modifier.isPrivate(Preconditions.class.getDeclaredConstructor().getModifiers())) { /* use static check methods */ }

Type guard

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

Try / catch

try { new Preconditions(); } catch (IllegalStateException e) { /* expected: static assertion helper */ }

Prevention

When it happens

Trigger: Calling new Preconditions() or reflecting it.

Common situations: DI/codegen instantiating all classes; developers mimicking instance-based validators.

Related errors


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

Appendix: source

Thrown at autosize/src/main/java/me/jessyan/autosize/utils/Preconditions.java:30

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.autosize.utils;

import android.os.Looper;

/**
 * ================================================
 * Created by JessYan on 26/09/2016 13:59
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public final class Preconditions {

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

    public static void checkArgument(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkArgument(boolean expression, Object errorMessage) {
        if (!expression) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs));
        }

View on GitHub (pinned to e402ecdd99)