1.15.2 NullPointerException error onMenuClick (Vault)

Discussion in 'Spigot Plugin Development' started by hsun00, Mar 2, 2020.

  1. Hi,
    I'm trying to learn how to hook onto vault and make a plugin that allows players to purchase ranks using Vault. When I click the item in the GUI, it gives this error:

    Code (Text):
    Could not pass event InventoryClickEvent to HoneyProject v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:541) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:528) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:2148) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PacketPlayInWindowClick.a(SourceFile:32) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at rankUp.RankGuiHandler.onMenuClick(RankGuiHandler.java:31) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85]
            ... 17 more
    Here is my Event handler class. I have tried different variants of checking if the player has enough money using different tutorials, but I get the same result. I beleive the issue is at
    Code (Text):
    if(economy.getBalance(p) > 500) {
    Code (Text):
    package rankUp;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;

    import me.hsun00.honeyproject.Main;
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;

    public class RankGuiHandler implements Listener{
       
        ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
        private Economy economy = Main.economy;

        @EventHandler
        public void onMenuClick(InventoryClickEvent e){
            Player p = (Player) e.getWhoClicked();
            if (e.getView().getTitle().equalsIgnoreCase(ChatColor.RED + "survivor")){
                e.setCancelled(true);
                if (e.getCurrentItem() == null){
                    return;
                }else if (e.getCurrentItem().getType().equals(Material.NETHER_STAR)){
               
                    if(p.hasPermission("honeyproject.member")) {
                       
                    if(economy.getBalance(p) > 500) {
             
                        String survivorcmd = "manuadd " + p.getName() + " survivor";
                        Bukkit.dispatchCommand(console, survivorcmd);
                    p.sendMessage(ChatColor.DARK_GREEN + "You have been ranked up!");
                    p.closeInventory();
                   
                    } else if (economy.getBalance(p) < 500){
                        p.sendMessage(ChatColor.RED + "You do not have enough money to purchase this!");
                        return;
                    }
                }
                   
                    else {
                    p.sendMessage(ChatColor.RED + "You cannot purchase Survivor");
                }
                }
            }
        }
       
       
       
        @EventHandler
        public void onMenuClick1(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if (e.getView().getTitle().equalsIgnoreCase(ChatColor.RED + "ultimatesurvivor")){
                e.setCancelled(true);
                if (e.getCurrentItem() == null){
                    return;
                }else if (e.getCurrentItem().getType().equals(Material.NETHER_STAR)){
               
                    if(p.hasPermission("honeyproject.survivor")) {
                        String ultimatesurvivor = "manuadd " + p.getName() + " ultimate_survivor";
                        Bukkit.dispatchCommand(console, ultimatesurvivor);
                    p.sendMessage(ChatColor.DARK_GREEN + "You have been ranked up!");
                    p.closeInventory();
                }else {
                    p.sendMessage(ChatColor.RED + "You cannot purchase Survivor");
                }
                }
            }
        }
       
       
       
       
        @EventHandler
        public void onMenuClick2(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if (e.getView().getTitle().equalsIgnoreCase(ChatColor.RED + "legendarysurvivor")){
                e.setCancelled(true);
                if (e.getCurrentItem() == null){
                    return;
                }else if (e.getCurrentItem().getType().equals(Material.NETHER_STAR)){
               
                    if(p.hasPermission("honeyproject.ultimatesurvivor")) {
                        String legendarysurvivor = "manuadd " + p.getName() + " legendary_survivor";
                        Bukkit.dispatchCommand(console, legendarysurvivor);
                    p.sendMessage(ChatColor.DARK_GREEN + "You have been ranked up!");
                    p.closeInventory();
                }else {
                    p.sendMessage(ChatColor.RED + "You cannot purchase Survivor");
                }
                }
            }
        }
       
       
       
       
       
       
    }
                   
                   
                 
                   
                   
                   
               
           
       
     

     
    Here is my main class:
    Code (Text):

    package me.hsun00.honeyproject;

    import org.bukkit.ChatColor;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import Items.TreeBreak;
    import net.milkbowl.vault.economy.Economy;
    import rankUp.RankGui;
    import rankUp.RankGuiHandler;

    public class Main extends JavaPlugin {
       
        public static Economy economy;
       
        @Override
        public void onEnable() {
            System.out.println(ChatColor.GREEN + "Successfully loaded HoneyProject plugin!");
            getCommand("tutorial").setExecutor(new tutorial());
            getCommand("survivor").setExecutor(new RankGui());
            getCommand("ultimatesurvivor").setExecutor(new RankGui());
            getCommand("legendarysurvivor").setExecutor(new RankGui());

            getServer().getPluginManager().registerEvents(new MenuHandler(), this);
            getServer().getPluginManager().registerEvents(new TreeBreak(), this);
            getServer().getPluginManager().registerEvents(new RankGuiHandler(), this);      
           
            if(!setupEconomy()) {
                System.out.println("Missing vault plugin!");
            }
           
           
        }
       
        private boolean setupEconomy() {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if(economyProvider !=null) {
                economy = economyProvider.getProvider();
            }
           
            return (economy != null);
           
        }
       
       
       
       
       
        @Override
        public void onDisable() {
            System.out.println(ChatColor.RED + "HoneyProject plugin has been disabled!");
           
        }





        }

     
    I am aware that my Java skills are lacking quite a bit, but I'm just trying to learn, so if anyone wants to help me I would greatly appreciate that. :) I just cant figure out what I'm doing wrong..
     
  2. In your onEnable, you must register the Economy class before registering the events, or it'll always be null for classes using it.