apache/pulsar · error · RuntimeException
User class must have a no-arg constructor
Error message
User class must have a no-arg constructor
What it means
createInstance requires a no-arg constructor: it calls getDeclaredConstructor() with no arguments. If the class has no accessible zero-argument constructor, NoSuchMethodException is wrapped in this RuntimeException. All classes instantiated through this helper must expose a public no-arg constructor.
Source
Thrown at pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java:92
if (!xface.isAssignableFrom(theCls)) {
throw new RuntimeException(userClassName + " does not implement " + xface.getName());
}
@SuppressWarnings("unchecked") // safe: theCls is verified to be assignable to xface
Class<T> tCls = (Class<T>) theCls.asSubclass(xface);
T result;
try {
@SuppressWarnings("unchecked") // safe: constructor cache is keyed by theCls which extends T
Constructor<T> meth = (Constructor<T>) constructorCache.get(theCls);
if (null == meth) {
meth = tCls.getDeclaredConstructor();
meth.setAccessible(true);
constructorCache.put(theCls, meth);
}
result = meth.newInstance();
} catch (InstantiationException ie) {
throw new RuntimeException("User class must be concrete", ie);
} catch (NoSuchMethodException e) {
throw new RuntimeException("User class must have a no-arg constructor", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("User class must have a public constructor", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("User class constructor throws exception", e);
}
return result;
}
/**
* Create an instance of <code>userClassName</code> using provided <code>classLoader</code>.
*
* @param userClassName user class name
* @param classLoader class loader to load the class.
* @return the instance
*/
public static Object createInstance(String userClassName,
ClassLoader classLoader) {View on GitHub (pinned to 820761864e)
Solutions
- Add a public no-arg constructor to the class
- Move required setup out of the constructor into an initialize/lifecycle method the framework calls
- If parameters are unavoidable, instantiate the object yourself instead of via this reflective helper
Example fix
// before
public MyFilter(String topic) { this.topic = topic; }
// after
public MyFilter() { }
public MyFilter(String topic) { this.topic = topic; } Defensive patterns
Strategy: validation
Validate before calling
// ensure a declared no-arg constructor exists
Class<?> cls = Class.forName(className, true, classLoader);
try {
cls.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Missing no-arg constructor: " + className);
} Type guard
boolean hasNoArgCtor(Class<?> cls) { try { cls.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return false; } } Try / catch
try {
T obj = Reflections.createInstance(className, XFace.class, classLoader);
} catch (RuntimeException e) {
if (e.getMessage().equals("User class must have a no-arg constructor")) {
throw new IllegalArgumentException("Add a public no-arg constructor to " + className, e);
}
throw e;
} Prevention
- Always keep an explicit public no-arg constructor in reflectively instantiated classes
- Use an init() method for dependencies instead of constructor injection
- Run a startup smoke test that instantiates every configured class
When it happens
Trigger: Configuring a class whose only constructors take arguments, so `theCls.getDeclaredConstructor()` fails with NoSuchMethodException.
Common situations: Plugin classes written with constructor injection; adding a constructor with parameters removed the implicit no-arg constructor; Kotlin/Scala classes without a default constructor.
Related errors
- Failed to instantiate ${className}
- Exception caused while converting configuration: ${message}
- Failed to compute configuration overrides
- Failed to create authentication: ${message}
- (wraps reflective instantiation failure for BrokerEntryMetad
AI-assisted analysis of apache/pulsar@820761864e (2026-09-06).
Data as JSON: /api/errors/f3e6e29e0e4a318f.
Report an issue: GitHub.