#Calling constructor of passed in <? extends class> arguement in a function

3 messages · Page 1 of 1 (latest)

molten gazelle
#

Hello friends. I have a function in which one of the parameters is Class<? extends Image> klass in which a class that extends image can be passed in to, the issue is, I am trying to call the constructor of that passed in klass, and create a new object of it. I am getting this error when I try to run it.

java.lang.NoSuchMethodException: world.ntdi.planium.manger.image.images.Longer.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3585) ~[na:na]
    at java.base/java.lang.Class.getConstructor(Class.java:2271) ~[na:na]
    at world.ntdi.planium.controller.PresetController.checkImageLocation(PresetController.java:43) ~[classes/:na]
    at world.ntdi.planium.controller.PresetController.createLong(PresetController.java:32) ~[classes/:na]```

Heres my code:
```java
private ImageLocation checkImageLocation(String text, Integer fontSize, Integer x, Integer y, String bufferedIO, Class<? extends Image> klass) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        String serialized = ImageSerialization.serializeName(fontSize, x, y, text, ImageIO.read(new File(bufferedIO)));

        if (cache.get(serialized) != null) {
            return new ImageLocation("localhost:8080/cache/" + cache.get(serialized).getFile().getName());
        }

        Constructor<? extends Image> constructor = klass.getConstructor();
        Image image = constructor.newInstance(text, fontSize, x, y);
        cache.put(image.getSerializeName(), image);
        return new ImageLocation("localhost:8080/cache/" + image.getFile().getName());
    }``` Let me know if there is any code I can send to help!
patent badgeBOT
#

This post has been reserved for your question.

Hey @molten gazelle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.