Java nosuchmethodexception when getting constructor

I tried to use reflection to load instances of classes When I tried this, I got an exception without this method I'll check and recheck That constructor obviously does exist Does anyone have any ideas? I've successfully used another project with basically the same code before, so I'm not sure I screwed up The source can be found here:

private void loadCommands() {
        try {
            for (Class<?> clazz : ReflectionsReplacement.getSubtypesOf(BaseCommand.class,"us.zsugano.itemsave.commands",plugin.getClass().getClassLoader(),BaseCommand.class)) {

                BaseCommand baseCommand = null;
                try {
                    baseCommand = (BaseCommand) clazz.getConstructor(ItemSave.class).newInstance(plugin);

                    if(Listener.class.isAssignableFrom(clazz)) {
                        plugin.getServer().getPluginManager().registerEvents((Listener) baseCommand,plugin);
                    }

                } catch (Exception e) {
                    plugin.PluginPM.sendMessage(Level.SEVERE,"Issues encountered when trying to load commands.");
                    e.printStackTrace();
                }
                commands.add(baseCommand);
            }
        } catch (Exception e) {
            plugin.PluginPM.sendMessage(Level.SEVERE,"Exception caught while loading commands.");
            e.printStackTrace();
        }

        for (BaseCommand command : commands) {
            plugin.getCommand(command.getName().toLowerCase()).setExecutor(this);
        }

}

public abstract class BaseCommand {

    public ItemSave plugin;

    public BaseCommand(ItemSave plugin) {
        this.plugin = plugin;
}

Full source: https://github.com/zachoooo/ItemSave

This is a stack trace:

19:43:10 [SEVERE] [ItemSave] Issues encountered when trying to load commands.
19:43:10 [SEVERE] java.lang.NoSuchMethodException: us.zsugano.itemsave.commands.
StoreCommand.<init>(us.zsugano.itemsave.ItemSave)
19:43:10 [SEVERE]       at java.lang.Class.getConstructor0(UnkNown Source)
19:43:10 [SEVERE]       at java.lang.Class.getConstructor(UnkNown Source)
19:43:10 [SEVERE]       at us.zsugano.itemsave.commands.CommandManager.loadComma
nds(CommandManager.java:32)
19:43:10 [SEVERE]       at us.zsugano.itemsave.commands.CommandManager.<init>(Co
mmandManager.java:23)
19:43:10 [SEVERE]       at us.zsugano.itemsave.ItemSave.onEnable(ItemSave.java:1
9)
19:43:10 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:217)
19:43:10 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
JavaPluginLoader.java:457)
19:43:10 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
mplePluginManager.java:381)
19:43:10 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin
(CraftServer.java:282)
19:43:10 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlug
ins(CraftServer.java:264)
19:43:10 [SEVERE]       at net.minecraft.server.v1_6_R2.minecraftServer.l(minecr
aftServer.java:313)
19:43:10 [SEVERE]       at net.minecraft.server.v1_6_R2.minecraftServer.f(minecr
aftServer.java:290)
19:43:10 [SEVERE]       at net.minecraft.server.v1_6_R2.minecraftServer.a(minecr
aftServer.java:250)
19:43:10 [SEVERE]       at net.minecraft.server.v1_6_R2.DedicatedServer.init(Ded
icatedServer.java:151)
19:43:10 [SEVERE]       at net.minecraft.server.v1_6_R2.minecraftServer.run(mine
craftServer.java:391)
19:43:10 [SEVERE]       at net.minecraft.server.v1_6_R2.ThreadServerApplication.
run(SourceFile:582)

resolvent

Solution

At storecommand In Java, I see the private constructor of this package:

StoreCommand(ItemSave plugin) {
  super(plugin);
}

API documentation from getconstructor (emphasize my):

Either set the constructor to public, or use getdeclaraedconstructor () and set setaccessible (true)

The above is all the contents of Java nosuchmethodexception when obtaining constructors collected and sorted by programming house for you. I hope this article can help you solve the program development problems encountered by Java nosuchmethodexception when obtaining constructors.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>