Hi, For some reason I keep getting an NPE on line 42 in my code which is using the bukkit method canEnchantItem(ItemStack) Here is my code Code:java public void enchant(Enchantment ench, int power, PlayerInventory pi, int price, Player p){ for(ItemStack is:pi.getContents()){ if(is == null) continue; //If the itemstack is null it's going to keep looping if(ench.canEnchantItem(is)){ if(power > ench.getMaxLevel()){ p.sendMessage(prefix + ChatColor.RED + "You've already enchanted your items with " + ench.toString().toLowerCase().replaceAll("_", " ") + " to the max!"); return; }else{ for(ItemStack i:pi.getContents()){ i.addEnchantment(ench, power); } plugin.econ.withdrawPlayer(p.getName(), price); p.sendMessage(prefix + ChatColor.GREEN + "Succesfully enchanted your items with " + ench.toString().toLowerCase().replaceAll("_", " ") + " for " + price + "!"); return; } } } for(ItemStack is:pi.getArmorContents()){ if(is == null) continue; if(ench.canEnchantItem(is)){ //NPE ON THIS LINE if(power > ench.getMaxLevel()){ p.sendMessage(prefix + ChatColor.RED + "You've already enchanted your items with " + ench.toString().toLowerCase().replaceAll("_", " ") + " to the max!"); return; }else{ for(ItemStack i:pi.getContents()){ i.addEnchantment(ench, power); } plugin.econ.withdrawPlayer(p.getName(), price); p.sendMessage(prefix + ChatColor.GREEN + "Succesfully enchanted your items with " + ench.toString().toLowerCase().replaceAll("_", " ") + " for " + price + "!"); return; } } } p.sendMessage(prefix + ChatColor.RED + "You have nothing in your inventory that can be enchanted with " + ench.toString().toLowerCase().replaceAll("_", " ") + "!"); } Stacktrace: Code: Caused by: java.lang.NullPointerException at net.minecraft.server.v1_7_R2.EnchantmentWeaponDamage.canEnchant(SourceFile:78) at org.bukkit.craftbukkit.v1_7_R2.enchantments.CraftEnchantment.canEnchantItem(CraftEnchantment.java:57) at org.bukkit.enchantments.EnchantmentWrapper.canEnchantItem(EnchantmentWrapper.java:46) at com.mydeblob.commands.CommandHandler.enchant(CommandHandler.java:42) at com.mydeblob.commands.CommandHandler.onCommand(CommandHandler.java:87) at org.bukkit.command.PluginCommand.execute(PluginCommand.java:50) ... 13 more I'm calling the method in a command.
PogoStick29 Sure. Command that uses enchant(); Code:java if(cmd.getName().equalsIgnoreCase("sharpness")){enchant(Enchantment.DAMAGE_ALL, this.getEnchLevel(p, Enchantment.DAMAGE_ALL) + 1, p.getInventory(), this.getEnchPrice(p, Enchantment.DAMAGE_ALL, "sharpness"), p);return true;} This command uses 2 other methods, getEnchLevel and getEnchPrice. The getEnchLevel is simply returning the next possible enchantment level of the item. It can be seen here getEnchLevel method: Code:java public int getEnchLevel(Player p, Enchantment ench){PlayerInventory pi = p.getInventory();for(ItemStack is:pi.getContents()){if(is != null && is.containsEnchantment(ench)){return is.getEnchantmentLevel(ench);}}for(ItemStack is:pi.getArmorContents()){if(is != null && is.containsEnchantment(ench)){return is.getEnchantmentLevel(ench);}}return 0;} The getEnchPrice just fetches the config.yml for a configurable price for the enchantment. Code:java public int getEnchPrice(Player p, Enchantment ench, String s){String path;if(getEnchLevel(p, ench) == 0){path = s + 1;return plugin.getConfig().getInt(path);}else{return plugin.getConfig().getInt(s + getEnchLevel(p, ench));}} Keep in mind everything is one up. For example, if I have sharpness 1 on a sword getEnchLevel is going to return sharpness 2. And getEnchPrice is going to return the price for sharpness 2.
Seems like the only thing that could possibly be null is the return for getEnchPrice(). Print out the result of that before you call the enchant() method and tell me if it's null.
PogoStick29 Ok. So I added this line of code Code: p.sendMessage("Enchant price" + this.getEnchPrice(p, Enchantment.DAMAGE_ALL, "sharpness")); It did indeed return the correct price, which is 10. This is the config.yml Code: #This is the config for the amount of money each enchantment upgrade costs sharpness1: 10 sharpness2: 10 sharpness3: 10 sharpness4: 10 sharpness5: 10 protection1: 10 protection2: 10 protection3: 10 protection4: 10 power1: 10 power2: 10 power3: 10 power4: 10 power5: 10 speed1: 10 speed2: 10 strength1: 10 strength2: 10
I'm pretty sure my stacktraces aren't that small, is that all of it? There should be somewhere stating the class and line that caused it.
itzrobotix at com.mydeblob.commands.CommandHandler.enchant(CommandHandler.java:42) = CommandHandler line 42. I know what you mean about stack traces from Bukkit being longer, but this is the important part.
I didn't post the full stacktrace. Just the relevant part. I'll just perform some more bug testing and try and fine the specific problem. Thanks for the help so far.
Sometimes the entire thing is relevant. It might be the fact that, for no apparent reason, getArmorContents returns air ItemStacks instead of null for empty slots. Try returning(er...continuing) if the type of the item is air.
I suppose the entire stacktrace is relevant. That's a bad habit of mine, (Only focusing on a small portion of the stacktrace) so I will work on improving this. Anyhow, that happened to work! For some reason, getArmorConetents returns the type of AIR if the slot is empty. I guess I just assumed it would be null, since getContents is null. Thanks for the help!