# Why does .getConstructor fail with a NoSuchMethodException despite zero-arg constructors existing for the class?

**URL:** <https://discourse.processing.org/t/why-does-getconstructor-fail-with-a-nosuchmethodexception-despite-zero-arg-constructors-existing-for-the-class/45748>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [February 12, 2025, 3:14pm UTC](https://discourse.processing.org/t/why-does-getconstructor-fail-with-a-nosuchmethodexception-despite-zero-arg-constructors-existing-for-the-class/45748 "2025-02-12T15:14:08Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![PeterAmbos](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/peterambos/32/20438_2.png) [@PeterAmbos](https://discourse.processing.org/u/PeterAmbos)\
**Post date:** [February 12, 2025, 6:09pm UTC](https://discourse.processing.org/t/why-does-getconstructor-fail-with-a-nosuchmethodexception-despite-zero-arg-constructors-existing-for-the-class/45748/6 "2025-02-12T18:09:16Z")

</div>

Thank you for linking me to those two topics - I’ve read them carefully and with some more research, I understood why I have to use `.getDeclaredConstructor` and give it the sketch’s class as an argument.

So now, following your implementation from this topic: [How do you choose what class you add to a list with a variable?](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987), I had added this to the beginning of my sketch go get its class:

```processing
final Class<? extends PApplet> sketchClass = getClass();

```

and changed the code in the method to

```processing
Ingredient ingredient = ingredientClass.getDeclaredConstructor(sketchClass).newInstance(this);

```

That got me an `IllegalArgumentException: argument type mismatch`, so I printed the stack trace of the Exception and got this:

```auto
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) 
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) 
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) 
at ice_creamillionaire$PlayerCharacter$Inventory.add(ice_creamillionaire.java:802) 
at ice_creamillionaire$PlayerCharacter.interact(ice_creamillionaire.java:771) 
at ice_creamillionaire.keyPressed(ice_creamillionaire.java:55) 
at processing.core.PApplet.keyPressed(PApplet.java:2772) 
at processing.core.PApplet.handlekeyEvent(PApplet.java:2636) 
at processing.core.PApplet.dequeueEvents(PApplet.java:2262) 
at processing.core.PApplet.handleDraw(PApplet.java:2104) 
at processing.awt.PSurfaceAWT$9.callDraw(PSurfaceAWT.java:1388) 
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:356) 

```

I checked the documentation of `newInstance` to see what the problem could be, and I read that the parameters of `newInstance` are

> an array of objects to be passed as arguments to the constructor call

Now knowing that, I thought that because I was calling zero-argument constructors, I should try calling `newInstance` without any arguments. I did that, but it got me an `IllegalArgumentException: wrong number of arguments` with the same stack trace. After that, I looked at the code in your implementations again, but I can’t figure out what I’m doing wrong, and I’d be very thankful for some more help.

---

_[View the full topic](https://discourse.processing.org/t/why-does-getconstructor-fail-with-a-nosuchmethodexception-despite-zero-arg-constructors-existing-for-the-class/45748)._
