我的插件(Bukkit for Minecraft)中存在一个问题,该问题使得此函数在HashMap类的.get()方法的第733行(我剪切了很多部分,线条分开)上抛出了NullPointerException。它也可能是以前的情况,我真的不知道或不了解发生了什么。在我检查.containsKey()之前,它在.get()上出错了。
private float getTempFromArmor(Player player) {
float armorValue = 0.0F;
ItemStack helmet = player.getEquipment().getHelmet();
ItemStack chestplate = player.getEquipment().getChestplate();
ItemStack leggings = player.getEquipment().getLeggings();
ItemStack boots = player.getEquipment().getBoots();
HashMap<Material, Float> helmetValues = new HashMap<Material, Float>();
helmetValues.put(Material.LEATHER_HELMET, 0.2F);
helmetValues.put(Material.IRON_HELMET, -0.2F);
helmetValues.put(Material.CHAINMAIL_HELMET, -0.1F);
helmetValues.put(Material.GOLD_HELMET, 0.1F);
helmetValues.put(Material.DIAMOND_HELMET, 0.0F);
HashMap<Material, Float> chestplateValues = new HashMap<Material, Float>();
chestplateValues.put(Material.LEATHER_CHESTPLATE, 0.3F);
chestplateValues.put(Material.IRON_CHESTPLATE, -0.3F);
chestplateValues.put(Material.CHAINMAIL_CHESTPLATE, -0.15F);
chestplateValues.put(Material.GOLD_CHESTPLATE, 0.15F);
chestplateValues.put(Material.DIAMOND_CHESTPLATE, 0.0F);
HashMap<Material, Float> leggingsValues = new HashMap<Material, Float>();
leggingsValues.put(Material.LEATHER_LEGGINGS, 0.2F);
leggingsValues.put(Material.IRON_LEGGINGS, -0.2F);
leggingsValues.put(Material.CHAINMAIL_LEGGINGS, -0.1F);
leggingsValues.put(Material.GOLD_LEGGINGS, 0.1F);
leggingsValues.put(Material.DIAMOND_LEGGINGS, 0.0F);
HashMap<Material, Float> bootsValues = new HashMap<Material, Float>();
bootsValues.put(Material.LEATHER_BOOTS, 0.1F);
bootsValues.put(Material.IRON_BOOTS, -0.1F);
bootsValues.put(Material.CHAINMAIL_BOOTS, -0.05F);
bootsValues.put(Material.GOLD_BOOTS, 0.05F);
bootsValues.put(Material.DIAMOND_BOOTS, 0.0F);
从这里
if (helmetValues.containsKey(helmet.getType()))
armorValue += helmetValues.get(helmet.getType());
到这里
if (chestplateValues.containsKey(chestplate.getType()))
armorValue += chestplateValues.get(chestplate.getType());
if (leggingsValues.containsKey(leggings.getType()))
armorValue += leggingsValues.get(leggings.getType());
if (bootsValues.containsKey(boots.getType()))
armorValue += bootsValues.get(boots.getType());
if (armorValue > 0) armorValue *= armorMax;
else armorValue *= armorMin;
return MathHelper.clamp_float(armorValue, armorMin, armorMax);
}
堆栈追踪:
[07:21:55 WARN]: [Hardcore Biomes] Task #4 for Hardcore Biomes v0.6.2 generated an exception
java.lang.NullPointerException
at name.sml.franky1223.hardcorebiomes.HardcoreBiomes.getTempFromArmor(HardcoreBiomes.java:733) ~[?:?]
at name.sml.franky1223.hardcorebiomes.HardcoreBiomes.access$10(HardcoreBiomes.java:703) ~[?:?]
at name.sml.franky1223.hardcorebiomes.HardcoreBiomes$1.run(HardcoreBiomes.java:169) ~[?:?]
at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:587) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
请帮助我,我很困惑。提前谢谢。
答案 0 :(得分:1)
此代码不应引发NullPointerException
if ( helmet != null ) {
Float helmetArmor = helmetValues.get(helmet.getType();
if ( helmetArmor != null ) {
armorValue += helmetArmor ;
}
}
也许你的玩家没戴头盔?
答案 1 :(得分:0)
提供所有代码的方法相同,我怀疑问题不太可能来自这部分:
helmetValues.get();
更有可能来自:
helmet.getType()
尝试将其更改为以下内容:
if (helmet != null && helmetValues.containsKey(helmet.getType())) {
armorValue += helmetValues.get(helmet.getType());
}