Question

I have a problem in my plugin (Bukkit for Minecraft) that makes this function throw a NullPointerException on line 733 (I cut many parts, the lines are separated), on the .get() method of the HashMap class. It could also be the condition just before, I don't really know or understand what's going on. Before I checked for .containsKey(), it errored on the .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);

From here

        if (helmetValues.containsKey(helmet.getType()))
            armorValue += helmetValues.get(helmet.getType());

To here

        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);
}

Stack trace:

[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]

Please help me, I am very confused. Thank you in advance.

Was it helpful?

Solution

This code should not raise NullPointerException

    if ( helmet != null ) {
Float helmetArmor = helmetValues.get(helmet.getType();
if ( helmetArmor != null ) {
     armorValue += helmetArmor ;
}

}

Maybe your player has not helmet?

OTHER TIPS

Providing all that code is in the same method, I suspect the problem is less likely to be coming from this part:

helmetValues.get();

and more likely to be coming from:

helmet.getType()

Try changing it to something like this:

if (helmet != null && helmetValues.containsKey(helmet.getType())) {
  armorValue += helmetValues.get(helmet.getType());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top