clojure/clojure · error · java.lang.IllegalArgumentException

No matching ctor found for

Error message

No matching ctor found for 

What it means

NewExpr filters c.getConstructors() by argument count; if no public constructor takes exactly args.count() parameters, compilation of the (new C ...) form is aborted. Fires when the argument arity matches no public constructor — wrong arg count, or the matching constructor is not public.

Solutions

  1. Check the class's available constructors and match arity and types
  2. Use type hints/casts so argument types match a constructor signature
  3. Verify the class name resolves to the intended class
  4. Consult javadoc for required constructor parameters

Example fix

;; before
(new StringBuilder 42)
;; after
(new StringBuilder (str 42))
Defensive patterns

Strategy: validation

Validate before calling

(defn ctor-arity-ok? [cls n]
  (some #(= n (count (.getParameterTypes ^java.lang.reflect.Constructor %)))
        (.getDeclaredConstructors cls)))

Try / catch

(try (new Foo 1 2)
  (catch IllegalArgumentException e
    (when (.startsWith (.getMessage e) "No matching ctor")
      (println "check ctor signature:" (.getMessage e)))))

Prevention

When it happens

Trigger: (new Foo 1 2) when Foo has no two-arg constructor; passing wrong arity or types so no ctor's parameter list matches after argcount filtering and matching.

Common situations: Mismatches between documented and actual constructor arity after library upgrades; calling (Foo.) on classes with required args; typed args not matching any overload due to nil or wrong boxed types.

Related errors


AI-assisted analysis of clojure/clojure@f3b143341d (2026-09-09). Data as JSON: /api/errors/f9447ce10f5f9b53. Report an issue: GitHub.

Appendix: source

Thrown at src/jvm/clojure/lang/Compiler.java:3106

	public NewExpr(Class c, IPersistentVector args, int line, int column) {
		this.args = args;
		this.c = c;
		Constructor[] allctors = c.getConstructors();
		ArrayList ctors = new ArrayList();
		ArrayList<Class[]> params = new ArrayList();
		ArrayList<Class> rets = new ArrayList();
		for(int i = 0; i < allctors.length; i++)
			{
			Constructor ctor = allctors[i];
			if(ctor.getParameterTypes().length == args.count())
				{
				ctors.add(ctor);
				params.add(ctor.getParameterTypes());
				rets.add(c);
				}
			}
		if(ctors.isEmpty())
			throw new IllegalArgumentException("No matching ctor found for " + c);

		int ctoridx = 0;
		if(ctors.size() > 1)
			{
			ctoridx = getMatchingParams(c.getName(), params, args, rets);
			}

		this.ctor = ctoridx >= 0 ? (Constructor) ctors.get(ctoridx) : null;
		if(ctor == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
			{
			RT.errPrintWriter()
              .format("Reflection warning, %s:%d:%d - call to %s ctor can't be resolved.\n",
                      SOURCE_PATH.deref(), line, column, c.getName());
			}
	}

	public Object eval() {
		Object[] argvals = new Object[args.count()];

View on GitHub (pinned to f3b143341d)