Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError when it is clearly there

General Info: I am using the Bukkit/Spigot API in version git-Spigot-1d14d5f-ba32592 (MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT), IntelliJ IDEA 14.1.3 and compile with its default compiler. The java jdk version is 1.8.0_25.

So when I try to call this class's constructor, it throws the runtime exception from the title.

Inventory menu class

package me.lakan.util.inventory;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused") // Util functionality is not always used
public class InventoryMenu implements Listener {

    private InventoryType type;
    private String title;

    private Map<Integer, MenuOption> options;

    // Constructors
    public InventoryMenu(InventoryType type, String title, JavaPlugin plugin) {

        this.options = new HashMap<Integer, MenuOption>();
        this.type = type;
        this.title = title;

        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }

    public boolean addOption(int position, MenuOption option) {
        boolean res = isPosEmpty(position);
        this.options.put(position, option);

        return res;
    }

    public void addOption(String name, int position, ItemStack icon) {
        addOption(position, new MenuOption(icon, name));
    }

    public boolean isPosEmpty(int position) {
        return !this.options.containsKey(position);
    }

    public void openFor(Player p) {
        // Create a new inventory
        Inventory inv = Bukkit.createInventory(p, this.type, this.title);

        // Fill all icons at their positions
        for (Map.Entry<Integer, MenuOption> key : this.options.entrySet()) {
            inv.setItem(key.getKey(), key.getValue().getIcon());
        }

        // If the inventory is a player inventory, update the player's
        if (inv.getType() == InventoryType.PLAYER) {
            p.getInventory().setContents(inv.getContents());
        }
        // For any openable inventory, just open it up
        else {
            p.openInventory(inv);
        }
    }

    /**
     * Listens for inventory clicks
     * If the inventory is a menu:
     * - Cancel movement
     * - Push event
     * - Close inventory if it should
     * @param e The actual event
     */
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onInventoryClick(InventoryClickEvent e) {

        // Prevent clicking if this inventory was clicked
        if (e.getClickedInventory().getName().equals(this.title)) {
            e.setCancelled(true);

            // Check for option
            if (this.options.containsKey(e.getRawSlot())) {
                // Get the option for this slot
                MenuOption option = this.options.get(e.getRawSlot());

                // Fill out an event and push it
                MenuClickEvent event = new MenuClickEvent((Player) e.getWhoClicked(), true, option.getName(), e.getRawSlot());
                Bukkit.getServer().getPluginManager().callEvent(event);

                // Now close inventory if not cancelled
                if (event.willCLose()) {
                    e.getWhoClicked().closeInventory();
                }
            }
        }
    }

    @SuppressWarnings("unused")
    public interface OptionClickEventHandler {
        public void onOptionClick(MenuClickEvent event);
    }
}

The item menu class

package me.lakan.test;

import me.lakan.util.inventory.InventoryMenu;
import me.lakan.util.inventory.MenuClickEvent;
import me.lakan.util.inventory.MenuOption;
import me.lakan.util.item.ItemBuilder;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryType;

public class ItemMenu implements Listener {
    private PluginEntry plugin;
    private InventoryMenu menu;

    // Commands
    private TestCommand testCmd;

    public ItemMenu(PluginEntry plugin) {

        Validate.notNull(this.plugin, "The plugin reference may not be null");
        this.plugin = plugin;

        this.menu = new InventoryMenu(InventoryType.CHEST, "" + ChatColor.DARK_GRAY + "Abilities", this.plugin);

        // Test
        this.menu.addOption(1,
                new MenuOption(
                        new ItemBuilder()
                                .amount(1)
                                .material(Material.RAW_FISH)
                                .name(ChatColor.LIGHT_PURPLE + "Test")
                                .lore(ChatColor.WHITE + "Click me")
                                .build(),
                        "TestIdentifier"));
        this.testCmd= new TestCmd(this.plugin);
}

    public void openFor(Player p) {
        this.menu.openFor(p);
    }



    @EventHandler(priority = EventPriority.NORMAL)
    public void onOptionClick(MenuClickEvent e) {
        // Test
        if (e.getName().equals("TestIdentifier")) {
            this.testCmd.executeFor(e.getWhoClicked());
        }
    }
}

Exception stack trace

[12:48:25] [Server thread/ERROR]: Error occurred while enabling Test v1.0 (Is it up to date?) java.lang.NoSuchMethodError: me.lakan.util.inventory.InventoryMenu.(Lorg/bukkit/event/inventory/InventoryType;Ljava/lang/String;Lorg/bukkit/plugin/java/JavaPlugin;)V

at me.lakan.test.ItemMenu.(ItemMenu.java:33) ~[?:?] at me.lakan.test.CommandParser.(CommandParser.java:20) ~[?:?] at me.lakan.test.PluginEntry.onEnable(PluginEntry.java:21) ~[?:?] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugin(CraftServer.java:356) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.craftbukkit.v1_8_R2.CraftServer.enablePlugins(CraftServer.java:316) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.r(MinecraftServer.java:416) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.k(MinecraftServer.java:382) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.a(MinecraftServer.java:337) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.DedicatedServer.init(DedicatedServer.java:257) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:522) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]

As you can see the constructor is there and is to my knowledge properly called. So is this error a mistake in my project setup, an API thing, or something entirely different?

The utility classes are in a seperate module and everything works if I paste them into my test plugin module, but not inside the other module. However any other constructor in any other class inside me.lakan.util.inventory can be called normally.

like image 499
redpandamonium Avatar asked Jul 18 '15 11:07

redpandamonium


People also ask

What causes Java Lang NoSuchMethodError?

NoSuchMethodError is a runtime error in Java which occurs when a method is called that exists at compile-time, but does not exist at runtime. The Java Garbage Collector (GC) cannot free up the space required for a new object, which causes a java. lang. OutOfMemoryError .

What is Exception in thread main Java Lang NoSuchMethodError?

NoSuchMethodError: main Exception in thread "main" can come due to various reasons like: 1) The class which you are trying to run doesn't have the main method. 2) The signature of the main method is not correct. See here for all possible signatures of the main method in Java.

How do you fix No class Def Found error?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.


1 Answers

The problem was in the project structure.

For the test artifact, I chose the module output of the util module. Changing it to the artifact output of the util module fixed it.

like image 84
redpandamonium Avatar answered Oct 14 '22 06:10

redpandamonium