apache/pulsar · error · InitializationException

Could not instantiate ${cls.getName()} either with or withou

Error message

Could not instantiate ${cls.getName()} either with or without construction parameter ${outer}: ${ex}

What it means

Pulsar's InnerClassFactory tries to instantiate a config/args class reflectively: first via a constructor taking the outer instance (for non-static inner classes), then via a no-arg declared constructor. If both attempts fail, it wraps the first exception in InitializationException describing both attempts. Typically the class lacks a usable no-arg constructor (e.g. non-static inner class or constructor requiring args).

Source

Thrown at pulsar-client-tools/src/main/java/org/apache/pulsar/internal/InnerClassFactory.java:53

    public <K> K create(final Class<K> cls) throws Exception {
        try {
            return defaultFactory.create(cls);
        } catch (Exception ex0) {
            try {
                Constructor<K> constructor = cls.getDeclaredConstructor(outer.getClass());
                return constructor.newInstance(outer);
            } catch (Exception ex) {
                try {
                    @SuppressWarnings("deprecation") // Class.newInstance is deprecated in Java 9
                    K result = cls.newInstance();
                    return result;
                } catch (Exception ex2) {
                    try {
                        Constructor<K> constructor = cls.getDeclaredConstructor();
                        return constructor.newInstance();
                    } catch (Exception ex3) {
                        throw new InitializationException("Could not instantiate " + cls.getName()
                                + " either with or without construction parameter " + outer + ": " + ex, ex);
                    }
                }
            }
        }
    }
}

View on GitHub (pinned to 820761864e)

Solutions

  1. Make the parameter class a static nested class (or top-level) so a no-arg constructor suffices.
  2. Ensure the class has a public no-arg constructor and does not throw during construction.
  3. Inspect the nested cause (ex/ex3) in the InitializationException to see why instantiation failed.
  4. If it must be an inner class, provide the correct outer instance so the outer-parameter constructor succeeds.

Example fix

// before
class MyArgs { }            // non-static inner class inside another class
// after
static class MyArgs { }     // static nested class, instantiable with no-arg ctor
Defensive patterns

Strategy: try-catch

Validate before calling

// Java: check the class is instantiable before use
static boolean instantiable(Class<?> c) {
  return java.lang.reflect.Modifier.isStatic(c.getModifiers()) || c.getDeclaringClass() == null;
}

Type guard

static boolean hasNoArgCtor(Class<?> c) { try { c.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return false; } }

Try / catch

try { T cfg = factory.create(cls, outer); } catch (InitializationException e) { LOG.error("cannot instantiate {}: {}", cls, e.getCause(), e); /* use a default instance or abort with a clear message */ }

Prevention

When it happens

Trigger: Passing a class to create() that is a non-static inner class without an accessible outer instance, a class whose no-arg constructor throws, or a class whose declared constructor is inaccessible (security/visibility) — the reflective newInstance calls fail.

Common situations: Defining CLI @Parameters classes as inner classes without static; constructor performing failing initialization (throwing in <init>); module/JDK access restrictions blocking setAccessible; typo passing the wrong Class object.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/1dad3c87c87be67f. Report an issue: GitHub.