Solved ItemStack error

Discussion in 'Plugin Development' started by TheFl4me, Jun 8, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    TheFl4me

    Hi i am trying to code a GUI which allows you to warp to places by clicking on the selected item.
    The Material and name of each item is defined in a config. I keep getting an error whenever i try opening the GUI.


    Code:
    Caused by: java.lang.NullPointerException
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.setWarpItem(WarpSelectorListener.java:100) ~[?:?]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.openWarpGUI(WarpSelectorListener.java:139) ~[?:?]
        at elite.minecraft.plugin.kitpvp.commands.WarpCommand.onCommand(WarpCommand.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer.performCommand(CraftPlayer.java:244) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at elite.minecraft.plugin.kitpvp.Utils.ItemClick(Utils.java:171) ~[?:?]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.WarpSelectorClick(WarpSelectorListener.java:29) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_71]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_71]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_71]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_71]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        ... 18 more
    
    Code:
    private ItemStack setWarpItem(String warpname, String warpitem, int slot, YamlConfiguration wlist, Inventory inv) {
            String path = wlist.getString(warpname);
            if(path.equalsIgnoreCase("none")) {  
                return null;
            } else {
                Material item = Material.getMaterial(warpitem);
                KitPvP.getPlugin().getUtils().rename0(new ItemStack(item), ChatColor.GOLD + path);      
                inv.setItem(slot, new ItemStack(item));
                return new ItemStack(item);
            }
        }
    Line 100 is line 3 here.
     
  2. Offline

    RingOfStorms

    Your yamlconfiguration is returning null for whatever your warp name is, in other words, that warpname doesn't exist.
     
  3. Offline

    TheFl4me

    Is it just me being completly stupid/blind? because i don't see it ._.

    Code:
    private ItemStack setWarpItem(String warpname, String warpitem, int slot, YamlConfiguration wlist, Inventory inv) {
            String path = wlist.getString(warpname);
            if(path.equalsIgnoreCase("none")) {   
                return null;
            }
            Material item = Material.getMaterial(warpitem);
            KitPvP.getPlugin().getUtils().rename0(new ItemStack(item), ChatColor.GOLD + path);       
            inv.setItem(slot, new ItemStack(item));
            return new ItemStack(item);
        }
       
        public void openWarpGUI(Player p) {
            ItemStack not = new ItemStack(Material.THIN_GLASS, 1);
            KitPvP.getPlugin().getUtils().rename0(not, " ");       
            for(int i = 0; i < 26; i++) {
                inv.setItem(i, not);
            }       
            setWarpItem(warpname_1, warpitem_1, 10, wlist, inv);
            setWarpItem(warpname_2, warpitem_2, 11, wlist, inv);
            setWarpItem(warpname_3, warpitem_3, 12, wlist, inv);
            setWarpItem(warpname_4, warpitem_4, 13, wlist, inv);
            setWarpItem(warpname_5, warpitem_5, 14, wlist, inv);
            setWarpItem(warpname_6, warpitem_6, 15, wlist, inv);
            setWarpItem(warpname_7, warpitem_7, 16, wlist, inv);
                   
            p.openInventory(inv);       
        }
       
        public void loadWarpGUIYML(File file, YamlConfiguration cfg) {       
            try {
                file.createNewFile();
                cfg.set("Warp_1.Name", "warp");
                cfg.set("Warp_1.Item", "none");
                cfg.set("Warp_2.Name", "warp");
                cfg.set("Warp_2.Item", "none");
                cfg.set("Warp_3.Name", "warp");
                cfg.set("Warp_3.Item", "none");
                cfg.set("Warp_4.Name", "warp");
                cfg.set("Warp_4.Item", "none");
                cfg.set("Warp_5.Name", "warp");
                cfg.set("Warp_5.Item", "none");
                cfg.set("Warp_6.Name", "warp");
                cfg.set("Warp_6.Item", "none");
                cfg.set("Warp_7.Name", "warp");
                cfg.set("Warp_7.Item", "none");
                cfg.save(file);
            } catch (IOException e) {
                System.out.println(KitPvP.getPlugin().getLanguage().Dnotfound);
            }
        }
     
    Last edited: Jun 9, 2015
  4. Offline

    TheFl4me

  5. What is "warpname_1" and "warpname_2", etc?

    FileConfiguration#get(String) is returning null, implying the config doesn't contain the key. And the key your inputting is "warpname_1", etc.
     
  6. Offline

    TheFl4me

    "warpname" is the name i assign to the item and which is the actual name of the warp.

    Here the entire class to avoid future problems:

    Code:
    package elite.minecraft.plugin.kitpvp.listeners;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import elite.minecraft.plugin.kitpvp.KitPvP;
    
    public class WarpSelectorListener implements Listener {
       
        String warpselector = ChatColor.AQUA + "Warp Selector";
       
        File WarpGUIList = new File(KitPvP.directory_warpgui);
        YamlConfiguration wlist = YamlConfiguration.loadConfiguration(WarpGUIList);
       
        String warpname_1 = wlist.getString("Warp_1.Name");
        String warpname_2 = wlist.getString("Warp_2.Name");
        String warpname_3 = wlist.getString("Warp_3.Name");
        String warpname_4 = wlist.getString("Warp_4.Name");
        String warpname_5 = wlist.getString("Warp_5.Name");
        String warpname_6 = wlist.getString("Warp_6.Name");
        String warpname_7 = wlist.getString("Warp_7.Name");
       
        String warpitem_1 = wlist.getString("Warp_1.Item");
        String warpitem_2 = wlist.getString("Warp_2.Item");
        String warpitem_3 = wlist.getString("Warp_3.Item");
        String warpitem_4 = wlist.getString("Warp_4.Item");
        String warpitem_5 = wlist.getString("Warp_5.Item");
        String warpitem_6 = wlist.getString("Warp_6.Item");
        String warpitem_7 = wlist.getString("Warp_7.Item");
       
        Inventory inv = Bukkit.createInventory(null, 27, ChatColor.AQUA + "Warps:");
       
        @EventHandler
        public void WarpSelectorClick(PlayerInteractEvent e) {
            KitPvP.getPlugin().getUtils().ItemClick(e, warpselector, "warp");
        }
       
        @EventHandler
        public void WarpSelctorDrop(PlayerDropItemEvent e) {
            KitPvP.getPlugin().getUtils().ItemDrop(e, warpselector);
        }
       
        @EventHandler
        public void WarpSelectorInsidePlayerInv(InventoryClickEvent e) {
            KitPvP.getPlugin().getUtils().ItemMove(e, warpselector);
        }
       
        @EventHandler
        public void PlayerDeathClearWarpSelectorDrop(PlayerDeathEvent e) {
            KitPvP.getPlugin().getUtils().ItemClearOnDeath(e, warpselector);
        }
       
        @EventHandler
        public void ClickWarpItem(InventoryClickEvent e) {       
            Player p = (Player)e.getWhoClicked();   
            if (e.getInventory().getName().equalsIgnoreCase(ChatColor.AQUA + "Warps:")) {
                if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR) {
                    return;
                }
                e.setCancelled(true);
                WarpOnClick(p, warpname_1, e);
                WarpOnClick(p, warpname_2, e);
                WarpOnClick(p, warpname_3, e);
                WarpOnClick(p, warpname_4, e);
                WarpOnClick(p, warpname_5, e);
                WarpOnClick(p, warpname_6, e);
                WarpOnClick(p, warpname_7, e);
            }
        }
       
        private void WarpOnClick(Player p, String warpname, InventoryClickEvent e) {
            File warpfile = new File(KitPvP.directory_warps + File.separator + warpname + ".yml");
            YamlConfiguration cfg = YamlConfiguration.loadConfiguration(warpfile);   
            if(e.getCurrentItem().getItemMeta().hasDisplayName()) {
                if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + warpname)) {               
                    if(warpfile.exists()) {
                        p.closeInventory();
                        String warp_msg = KitPvP.getPlugin().getLanguage().Warp_warp.replaceAll("%w", warpname);
                        KitPvP.getPlugin().getWarpListener().Warp(p, warp_msg, cfg);
                        return;
                    } else {
                        p.sendMessage(KitPvP.getPlugin().getLanguage().Warp_nowarp);
                        return;
                    }
                }
            }
        }
       
        private ItemStack setWarpItem(String warpname, String warpitem, int slot, YamlConfiguration wlist, Inventory inv) {
            String path = wlist.getString(warpname);
            if(path.equalsIgnoreCase("none")) {   
                return null;
            }
            Material item = Material.getMaterial(warpitem);
            KitPvP.getPlugin().getUtils().rename0(new ItemStack(item), ChatColor.GOLD + path);       
            inv.setItem(slot, new ItemStack(item));
            return new ItemStack(item);
        }
       
        public void openWarpGUI(Player p) {
            ItemStack not = new ItemStack(Material.THIN_GLASS, 1);
            KitPvP.getPlugin().getUtils().rename0(not, " ");       
            for(int i = 0; i < 26; i++) {
                inv.setItem(i, not);
            }       
            setWarpItem(warpname_1, warpitem_1, 10, wlist, inv);
            setWarpItem(warpname_2, warpitem_2, 11, wlist, inv);
            setWarpItem(warpname_3, warpitem_3, 12, wlist, inv);
            setWarpItem(warpname_4, warpitem_4, 13, wlist, inv);
            setWarpItem(warpname_5, warpitem_5, 14, wlist, inv);
            setWarpItem(warpname_6, warpitem_6, 15, wlist, inv);
            setWarpItem(warpname_7, warpitem_7, 16, wlist, inv);
           
            p.openInventory(inv);       
        }
       
        public void loadWarpGUIYML(File file, YamlConfiguration cfg) {       
            try {
                file.createNewFile();
                cfg.set("Warp_1.Name", "warp");
                cfg.set("Warp_1.Item", "none");
                cfg.set("Warp_2.Name", "warp");
                cfg.set("Warp_2.Item", "none");
                cfg.set("Warp_3.Name", "warp");
                cfg.set("Warp_3.Item", "none");
                cfg.set("Warp_4.Name", "warp");
                cfg.set("Warp_4.Item", "none");
                cfg.set("Warp_5.Name", "warp");
                cfg.set("Warp_5.Item", "none");
                cfg.set("Warp_6.Name", "warp");
                cfg.set("Warp_6.Item", "none");
                cfg.set("Warp_7.Name", "warp");
                cfg.set("Warp_7.Item", "none");
                cfg.save(file);
            } catch (IOException e) {
                System.out.println(KitPvP.getPlugin().getLanguage().Dnotfound);
            }
        }
       
        public void giveWarpSelector(Player p) {       
            ItemStack warpselec = new ItemStack(Material.COMPASS);
            ItemMeta warp = warpselec.getItemMeta();
            warp.setDisplayName(warpselector);
            warpselec.setItemMeta(warp);
            p.getInventory().setItem(8, warpselec);
        }
    }
     
  7. Offline

    Zombie_Striker

    @TheFl4me
    The error message does not seem to go with your class. Line 100 in this new class is an end bracket, when it's an itemstack that is null which is the problem. Also, the error message says line 139 is also apart of the itemstack problem, yet that doesn't extend anything.
     
  8. Offline

    TheFl4me

    Because i tried fixing the class i had to change 1 or 2 things (didn't work) here is the "Updated" error msg:
    (srry forgot)

    Code:
    Caused by: java.lang.NullPointerException
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.setWarpItem(WarpSelectorListener.java:106) ~[?:?]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.openWarpGUI(WarpSelectorListener.java:121) ~[?:?]
        at elite.minecraft.plugin.kitpvp.commands.WarpCommand.onCommand(WarpCommand.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer.performCommand(CraftPlayer.java:244) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at elite.minecraft.plugin.kitpvp.Utils.ItemClick(Utils.java:165) ~[?:?]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.WarpSelectorClick(WarpSelectorListener.java:50) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_71]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_71]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_71]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_71]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        ... 18 more
    
     
  9. Offline

    Zombie_Striker

    106
    121

    Path is null somehow, Check if path is null, check if wlist is null. check if warpname is null. You know something is null, so find out which variable it is.
     
  10. Offline

    TheFl4me

    I know but like i said in my reply above:

    EDIT:

    Ok i found it this is how it is now:

    Code:
    private ItemStack setWarpItem(String warpname, String warpitem, int slot, YamlConfiguration wlist, Inventory inv) {
            if(warpname.equalsIgnoreCase("none")) {  
                return null;
            }
            Material mat = Material.getMaterial(warpitem);
            ItemStack item = new ItemStack(mat);
            KitPvP.getPlugin().getUtils().rename0(item, ChatColor.GOLD + warpname);      
            inv.setItem(slot, item);
            return item;
        }
    but now im getting a new error.

    Code:
    Caused by: java.lang.NullPointerException
        at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:46) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.setWarpItem(WarpSelectorListener.java:109) ~[?:?]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.openWarpGUI(WarpSelectorListener.java:121) ~[?:?]
        at elite.minecraft.plugin.kitpvp.commands.WarpCommand.onCommand(WarpCommand.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer.performCommand(CraftPlayer.java:244) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at elite.minecraft.plugin.kitpvp.Utils.ItemClick(Utils.java:165) ~[?:?]
        at elite.minecraft.plugin.kitpvp.listeners.WarpSelectorListener.WarpSelectorClick(WarpSelectorListener.java:50) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_71]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_71]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_71]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_71]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        ... 18 more
    

    EDIT AGAIN: i fixed it.
     
    Last edited: Jun 9, 2015
Thread Status:
Not open for further replies.

Share This Page