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. Why does this happen?

Jon Skeet
people
quotationmark

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. 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.

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.

people

See more on this question at Stackoverflow