简体   繁体   English

使用newInstance()实例化类时出现NoSuchMethodException

[英]NoSuchMethodException when instantiating a class using newInstance()

I'm using the following code to instantiate every class in a package: 我正在使用以下代码实例化包中的每个类:

        for (Class listenerClass : ClassGetter.getClassesForPackage(this, "cullan.listener")) {
        Bukkit.getLogger().info(listenerClass.getSimpleName());
        if (!listenerClass.getSimpleName().equals("EnchantmentListener")) {
            try {
                listenerClass.newInstance();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

This works perfectly fine, however, I'm getting this error when one of the classes is being instantiated: 这工作得很好,但是,当实例化其中一个类时出现此错误:

[07:07:58] [Server thread/WARN]: java.lang.InstantiationException: cullan.listener.AgilityListener$1
    [07:07:58] [Server thread/WARN]:    at java.lang.Class.newInstance(Unknown Source)
    [07:07:58] [Server thread/WARN]:    at cullan.main.DCEnchants.loadEnchants(DCEnchants.java:62)
    [07:07:58] [Server thread/WARN]:    at cullan.main.DCEnchants.onEnable(DCEnchants.java:24)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
    [07:07:58] [Server thread/WARN]:    at net.skycraftmc.PluginManager.PluginManagerPlugin.onCommand(PluginManagerPlugin.java:287)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:646)
    [07:07:58] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:632)
    [07:07:58] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:409)
    [07:07:58] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:373)
    [07:07:58] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:709)
    [07:07:58] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:612)
    [07:07:58] [Server thread/WARN]:    at java.lang.Thread.run(Unknown Source)
    [07:07:58] [Server thread/WARN]: Caused by: java.lang.NoSuchMethodException: cullan.listener.AgilityListener$1.<init>()
    [07:07:58] [Server thread/WARN]:    at java.lang.Class.getConstructor0(Unknown Source)

This is the class that is throwing the error: 这是引发错误的类:

    package cullan.listener;
import cullan.type.EnchantmentRarity;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;

import java.util.ArrayList;
import java.util.List;

public class AgilityListener extends EnchantmentListener{

    private List<Player> players = new ArrayList<>();

    public AgilityListener() {
        super(Enchantment.getByName("Agility"), EnchantmentRarity.VERY_RARE);
        startTimer();
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        if (players.contains(player)) {
            players.remove(player);
        }
    }

    private void startTimer() {
        final PotionEffect effect = mainConfig.getAgilityEffect();
        instance.getServer().getScheduler().scheduleSyncRepeatingTask(instance, new Runnable() {
            @Override
            public void run() {
                for (Player player : instance.getServer().getOnlinePlayers()) {
                    ItemStack boots = player.getInventory().getBoots();
                    if (boots != null && boots.containsEnchantment(enchantment)) {
                        player.addPotionEffect(effect);
                        players.add(player);
                    } else if (players.contains(player)) {
                        player.removePotionEffect(effect.getType());
                        players.remove(player);
                    }
                }
            }
        }, 10L, 10L);
    }
}

The error does not occur if I remove the startTimer() method. 如果删除startTimer()方法,则不会发生该错误。 Why does this happen? 为什么会这样?

The problem is that you're not trying to construct an instance of AgilityListener - you're trying to construct an instance of the anonymous inner class created in AgilityListener.startTimer . 问题是您没有尝试构建AgilityListener的实例, AgilityListener试图构建在AgilityListener.startTimer创建的匿名内部类的实例。 That's why the stack trace has: 这就是为什么堆栈跟踪具有:

Caused by: java.lang.NoSuchMethodException: cullan.listener.AgilityListener$1.<init>()

The "$1" shows that it's an anonymous inner class. “ $ 1”表明它是一个匿名内部类。

If you just want to filter out all nested/inner classes, you could expand your check on the class name: 如果只想过滤掉所有嵌套/内部类,则可以扩展对类名的检查:

if (!listenerClass.getSimpleName().equals("EnchantmentListener") &&
    !listenerClass.getSimpleName().contains("$")) 

Or: 要么:

!listenerClass.isAnonymousClass()

etc. You could also check whether the class has a public parameterless constructor, and that it's assignable to EnchantmentListener . 等等。您还可以检查类是否具有公共的无参数构造函数,以及该类是否可分配给EnchantmentListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM