NullPointerException With My Own Shop Plugin

Discussion in 'Spigot Plugin Development' started by QuantumNuke75, Apr 24, 2017.

?

Am I a hopeless coder?

  1. Yes

    1 vote(s)
    33.3%
  2. No

    2 vote(s)
    66.7%
  1. I am a novice coder, and I need help. I am creating a shop GUI within my plugin, however, I have an error. Also point anything else out if you want, I will happily make changes. If there are any answers(I am sure I am missing something), please explain to me in detail what I should change. I have also used Build Path for the Vault Plugin. So here is my code:
    Code (Text):

    package me.bukkit.quantum.event.inventory;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;

    import net.md_5.bungee.api.ChatColor;

    public class InventoryClick implements Listener{
        private static Economy econ = null;
        private static Permission perms = null;
        private static Chat chat = null;
    -------------------> START OF NOT IMPORTANT <--------------------------
        [USER=17846]@EventHandler[/USER]
        public void onInventoryClick(InventoryClickEvent event){
            Inventory inv = event.getInventory();
            if (!inv.getTitle().equals("Warp Menu"))
                return;
            if(!(event.getWhoClicked() instanceof Player))
                return;
            Player player = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
            if(item.getType() == Material.COMPASS){
                Location l = new Location(Bukkit.getWorld("Factions"), -512.491, 86 , -303.545);
                player.sendMessage(ChatColor.BOLD + "You have been teleported to Spawn!");
                player.teleport(l);
           
            }
            if(item.getType() == Material.DIAMOND_SWORD){
                /*Location l = new Location(Bukkit.getWorld("world"), -812.5, 71 , 5640.5);
                player.sendMessage(ChatColor.BOLD + "You have been teleported to the PvP Hub!");
                player.teleport(l);    */  
               
            }
            event.setCancelled(true);
            player.closeInventory();

       
        }
        [USER=17846]@EventHandler[/USER]
        public void onInventoryClicked(InventoryClickEvent event){
            Inventory inv = event.getInventory();
            if (!inv.getTitle().equals("NameTags"))
                return;
            if(!(event.getWhoClicked() instanceof Player))
                return;
            Player player = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
            if(item.getType() == Material.BOW){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " +  ChatColor.BLUE + "" + ChatColor.BOLD +  "[QuickShot]" );
            }
            if(item.getType() == Material.ARROW){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " + ChatColor.RED + "" + ChatColor.BOLD +  "[ArrowSpammer]" );
            }
            if(item.getType() == Material.BARRIER){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " +  "\"\"");
            }
            if(item.getType() == Material.IRON_SWORD){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " + ChatColor.DARK_GRAY + "" + ChatColor.BOLD +  "[BackStabber]" );
            }
            if(item.getType() == Material.DIAMOND_SWORD){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " + ChatColor.AQUA + "" + ChatColor.BOLD +  "[QuickDropper]" );
            }
            if(item.getType() == Material.EGG){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " + ChatColor.GOLD + "" + ChatColor.BOLD +  "[Harambe]" );
            }
            if(item.getType() == Material.GOLD_SWORD){
                player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " suffix " + ChatColor.DARK_GRAY + "" + ChatColor.BOLD +  "[Z3R0]" );
            }
            event.setCancelled(true);
            player.closeInventory();

       
        }            -------------------> END OF NOT IMPORTANT <--------------------------
        [USER=17846]@EventHandler[/USER]
        public void onInventoryClickes(InventoryClickEvent event){
            Inventory inv = event.getInventory();
            if (!inv.getTitle().equals("Buy Shop"))
                return;
            if(!(event.getWhoClicked() instanceof Player))
                return;
            Player player = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
            if(item.getType() == Material.TNT ){
                if(econ.getBalance(player) >= 1000){
                    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "eco take " + player.getName() + " 1000");
                    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "give " + player.getName() + " tnt 16");
                    event.setCancelled(true);
                }
            }
            if(item.getType() == Material.REDSTONE ){
                if(econ.getBalance(player) >= 128){
                    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "eco take " + player.getName() + " 128");
                    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "give " + player.getName() + " redstone 16");
                    event.setCancelled(true);
                }

            }
            if(item.getType() == Material.REDSTONE ){
                if(econ.getBalance(player) >= 128){
                    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "eco take " + player.getName() + " 128");
                    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "give " + player.getName() + " redstone 16");
                    event.setCancelled(true);
                }
            }
            if(item.getType() == Material.BARRIER){
                event.setCancelled(true);
                player.closeInventory();
            }
        }
    }
     

    Here is the error:
    24.04 16:33:40 [Server] ERROR Could not pass event InventoryClickEvent to EverythingPlugin v1.5 24.04 16:33:40 [Server] INFO org.bukkit.event.EventException 24.04 16:33:40 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_111] 24.04 16:33:40 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_111] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111] 24.04 16:33:40 [Server] INFO Caused by: java.lang.NullPointerException 24.04 16:33:40 [Server] INFO at me.bukkit.quantum.event.inventory.InventoryClick.onInventoryClickes(InventoryClick.java:105) ~[?:?] 24.04 16:33:40 [Server] INFO at sun.reflect.GeneratedMethodAccessor6098.invoke(Unknown Source) ~[?:?] 24.04 16:33:40 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111] 24.04 16:33:40 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111] 24.04 16:33:40 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8-latest.jar:git-Spigot-21fe707-e1ebe52] 24.04 16:33:40 [Server] INFO ... 15 more
     
    #1 QuantumNuke75, Apr 24, 2017
    Last edited: Apr 24, 2017
  2. maldahleh

    Wiki Team

    I won't comment on the rest of your code, and it is hard to read since you did not preserve formatting or indicate where line 105 is. Your issue is likely though that you don't check whether the item and inventory is null or not.
     
  3. Yep what's line 105 of inventoryclick.java?


    Sent from my iPhone using Tapatalk
     
  4. Line 105: if(econ.getBalance(player) >= 128){
    I was trying to check that if the player has a balance greater than 128, a transaction can occur.
     
  5. maldahleh

    Wiki Team

    Your econ variable is null:
    Code (Java):
    private static Economy econ = null;
    Where are you initializing it?
     
  6. I am initializing in the main class:

    Code (Text):

    private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
    And the full main class:
    Code (Text):

    package me.bukkit.quantum;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.bukkit.quantum.commands.Menu;
    import me.bukkit.quantum.commands.NameTagMenu;
    import me.bukkit.quantum.commands.Factions;
    import me.bukkit.quantum.commands.Hello;
    import me.bukkit.quantum.commands.Hub;
    import me.bukkit.quantum.commands.Info;
    import me.bukkit.quantum.commands.KillMe;
    import me.bukkit.quantum.commands.Lobby;
    import me.bukkit.quantum.commands.Noose;
    import me.bukkit.quantum.commands.Shop;
    import me.bukkit.quantum.event.block.BlockBreak;
    import me.bukkit.quantum.event.inventory.InventoryClick;
    import me.bukkit.quantum.event.player.PlayerChat;
    import me.bukkit.quantum.event.player.PlayerLoginPlayerMax;
    import net.md_5.bungee.api.ChatColor;

    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;

    public class MyFirstPlugin extends JavaPlugin {
        private static Economy econ = null;
        private static Permission perms = null;
        private static Chat chat = null;
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();

            logger.info(pdfFile.getName() + " has been enabled! (V." + pdfFile.getVersion() + ")");

            registerCommands();
            registerEvents();
            registerConfig();
            setupPermissions();
            setupChat();
            if (!setupEconomy() ) {
                   getServer().getPluginManager().disablePlugin(this);
                   return;
            }

        }

        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();

            logger.info(pdfFile.getName() + " has been disabled! (V." + pdfFile.getVersion() + ")");

        }

        public void registerCommands() {
            getCommand("killme").setExecutor(new KillMe());
            getCommand("ephello").setExecutor(new Hello());
            getCommand("factions").setExecutor(new Factions());
            getCommand("myinfo").setExecutor(new Info());
            getCommand("hub").setExecutor(new Hub());
            getCommand("noose").setExecutor(new Noose());
            getCommand("menu").setExecutor(new Menu());
            getCommand("lobby").setExecutor(new Lobby());
            getCommand("nametags").setExecutor(new NameTagMenu());
            getCommand("shop").setExecutor(new Shop());

           
           

        }

        public void registerEvents() {

            PluginManager pm = getServer().getPluginManager();

            pm.registerEvents(new BlockBreak(), this);
            pm.registerEvents(new PlayerChat(), this);
            pm.registerEvents(new InventoryClick(), this);
            pm.registerEvents(new PlayerLoginPlayerMax(), this);

        }
       
        private void registerConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
       
        private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }
       
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = rsp.getProvider();
            return perms != null;
        }



    }

     
     
  7. U'll want to initialize the eco class or at least grab the instance from the core class if you have initialised it from there EG: econ.getInstance();
    or plugin.getEco();
     
  8. Sorry, don't quite get what you are saying. (Novice at work)
     
  9. maldahleh

    Wiki Team

    I won't comment on the rest of your code but you are referencing the econ variable located in your listener class (which is null) and not the one of your main class which you initialized, you can't access the one of your main class though since you have it set to private so you should change that to public and access it by referencing your main class.

    In reality though, you should drop the static for the most part and pass an instance of your main plugin to the constructor of your listener class, then use the passed instance to reference your economy integration.
     
  10. You're registering events before you initialize your economy.
    I'd recommend passing an instance of your main class into your listener, as using static in this way is bad practice.

    In your InventoryClick class, create a constructor which accepts an instance of "MyFirstPlugin"
    Here's the Java tutorial on using constructors: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

    To access the economy from your listener class, you'll need to reference it from the instance of your main class you passed.
    The instance will be only in the scope of your constructor, therefore you will need to assign the instance to a variable within the scope of the class, which could then be accessed from your listener method.

    Code (Text):
    // outside constructor
    private MyFirstPlugin instance;

    // constructor
    public InventoryClick(MyFirstPlugin instance) {
        this.instance = instance;
    }
    You can then call non-static public variables and methods from your main class within your listener class. This includes your economy variable, which will have a value as long as you call the constructor of your InventoryClick class after you setup the economy.
     
    • Agree Agree x 1