NullPointerException

Discussion in 'Plugin Development' started by Jeff.Halbert, Jun 26, 2012.

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

    Jeff.Halbert

    can anyone see why i'm getting a nullPointerException in this code? The stacktrace points to
    addChants() on line "stack.addEnchantment(ench, level);"
    Code:
        public static void giveKit(KitKat plugin, Player player, String kitName) {
            List<String> itemList = plugin.getConfig().getStringList("kits." + kitName + ".items");
            ListIterator<String> it = itemList.listIterator();
            if (it.hasNext()) {
                while (it.hasNext()) {
                    String[] item = it.next().split(" ");
                    int id = Integer.parseInt(item[0]);
                    short meta = (short) Integer.parseInt(item[1]);
                    int amount = Integer.parseInt(item[2]);
                    ItemStack stack = new ItemStack(id, amount, meta);
                    if (plugin.getConfig().getConfigurationSection("kits." + kitName + ".enchants").getKeys(false).contains(item[0])) {
                        ItemStack chantedStack = addChants(plugin, stack, kitName, item[0]);
                        player.getInventory().addItem(chantedStack);
                    } else {
                        player.getInventory().addItem(stack);
                    }
                }
            }
        }
       
        public static ItemStack addChants(KitKat plugin, ItemStack stack, String kitName, String item) {
            List<String> list = plugin.getConfig().getStringList("kits." + kitName + ".enchants." + item);
            ListIterator<String> it = list.listIterator();
            if (it.hasNext()) {
                while (it.hasNext()) {
                    String[] chant = it.next().split(" ");
                    Enchantment ench = Enchantment.getByName(chant[0]);
                    int level = Integer.parseInt(chant[1]);
                    stack.addEnchantment(ench, level);
                }
            }
            return stack;
        }
    Also, I got a null pointer exception when I used..
    Code:
    if (ench.canEnchantItem(stack)) {
        //this gave null.....
    }
    // or when I do this....
    int max = ench.getMaxLevel();
    int min = ench.getStartLevel();
    if ((level < min) || (level > max)) {
        //this gave null aswell......
    }
     
Thread Status:
Not open for further replies.

Share This Page