Console error

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 12, 2016.

  1. So, I'm getting this error in my console, but can't find anything wrong with my code. I believe it has something to do with my Main class and Cosmetic class. I'll post the code you need! Thanks Max.
    Code (Text):
    [05:44:30 ERROR]: Error occurred while enabling MainPlugin v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.MaxKrissigo.MainPlugin.Cosmetic.<init>(Cosmetic.java:39) ~[?:?]
        at me.MaxKrissigo.MainPlugin.Main.onEnable(Main.java:42) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.loadPlugin(CraftServer.java:361) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.enablePlugins(CraftServer.java:321) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.reload(CraftServer.java:745) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.Bukkit.reload(Bukkit.java:539) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:631) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:438) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:401) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
     
     
  2. java.lang.NullPointerException
    at me.MaxKrissigo.MainPlugin.Cosmetic.<init>(Cosmetic.java:39) ~[?:?]
    at me.MaxKrissigo.MainPlugin.Main.onEnable(Main.java:42) ~[?:?]

    Show us your code, specify these lines
     
  3. This is the relevant code in the Main class:
    Code (Text):

       
        private Menu menu;
        private Cosmetic cosmetic;
        private Kit kit;
        private Enchant enchant;
       
        HashMap<String, Scoreboard> playerSb = new HashMap<String,Scoreboard>();
       
        @Override
        public void onEnable() {
           
                menu = new Menu(this);
                kit = new Kit(this);
                enchant = new Enchant(this);
                cosmetic = new Cosmetic(this); //this is line 42
               
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
               
                if (!Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays")) {
                    getLogger().severe("*** HolographicDisplays is not installed or not enabled. ***");
                    getLogger().severe("*** This plugin will be disabled. ***");
                    this.setEnabled(false);
                    return;
                }

               
            }    
    And this is the relevant code of the cosmetic class.
    Code (Text):
        HashMap<String, Boolean> Hologram = new HashMap<String, Boolean>();
       
            private Inventory inv;
            private Inventory inv1;
            private Inventory inv2;
            private ItemStack killMessage, killDisplay, hologram;
           
            public Cosmetic(Plugin p) {
               
                    inv1 = Bukkit.getServer().createInventory(null, 45, "Kill Messages");
                    inv2 = Bukkit.getServer().createInventory(null, 45, "Kill Displays");
                    inv = Bukkit.getServer().createInventory(null, 45, "Cosmetic");
                   
                    if (Hologram.get(p.getName())) { //this is line 39
                       
                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.GREEN + "[ENABLED]");
                       
                    } else if (!Hologram.get(p.getName())) {
                       
                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.RED +  "[DISABLED]");
                       
                    }
                   
                    killMessage = createItem(Material.DIAMOND_SWORD, ChatColor.GOLD + "Kill Message");
                    killDisplay = createItem1(Material.GLASS, ChatColor.RED + "Kill Display");
                   
                    inv2.setItem(12, hologram);
                    inv.setItem(20, killMessage);
                    inv.setItem(24, killDisplay);
                   
                    Bukkit.getServer().getPluginManager().registerEvents(this, p);
            }
     
  4. Can you show the which is line 39 in your cosmetic class?
     
  5. Ok, first of all this is your constructor, I am pretty sure you didn't mean to put all that code in the constructor :p

    if (Hologram.get(p.getName())) { //this is line 39

    You don't have a player object, or if you do, it is null.
    If you are trying to listen to clicks in your GUI, use InventoryClickEvent
     
  6. How would I get this to return true, so to check if the player is true? I've set up a hash map to store the players.

    EDIT: Basically check whether the boolean is true or not for that player.
     
  7. Code (Text):

        private Main plugin;
       
        public Cosmetic (Main pl){
            plugin = pl;
        }
    This is what you want inside your constructor I guess. Not all that code, all that code should go to here:

    Code (Text):
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e){
    //your code inside your constructor
    }
    Get the player from your hashmap if you want but you can get who clicked by e.getWhoClicked();
     
  8. So I've added this to my InventoryClickEvent, but it isn't doing anything.
    Code (Text):
            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                    if (e.getCurrentItem().getItemMeta() == null) return;
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Hologram")) {
                        e.setCancelled(true);
                        Player p = (Player) e.getWhoClicked();
                        if (Hologram.get(p.getName())) {
                           
                            hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.RED +  "[DISABLED]");
                           
                            Hologram.put(p.getName(), false);
                            p.sendMessage("Disabled");
                           
                        } else if (!Hologram.get(p.getName())) {

                            hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.GREEN + "[ENABLED]");
                           
                            Hologram.put(p.getName(), true);
                            p.sendMessage("Enabled");
                        }
                }
     
  9. Debug your code by sending the player some messages. Anything on the console?
     
  10. No nothing on the console.
     
  11. Send some messages here and there and see where the code doesn't work, then try to fix it.
     
  12. I have no idea how to fix it, but the code which checks whether the hologram item has clicked is not working.