Solved ItemMeta#getAttributeModifiers() throwing NullPointerException

Discussion in 'Plugin Development' started by X21_Eagle_X21, Jul 24, 2020.

  1. Offline

    X21_Eagle_X21

    I'm trying to add attribute modifiers to a leather helmet, but the following line throws a NullPointerException and I can't figure out why.

    Code:
    helmetMeta.getAttributeModifiers(Attribute.GENERIC_ARMOR).add(new AttributeModifier("generic.armor", 2, Operation.ADD_NUMBER));
    Code:
    java.lang.NullPointerException: null at org.bukkit.craftbukkit.v1_16_R1.inventory.CraftMetaItem.getAttributeModifiers(CraftMetaItem.java:994) ~[server.jar:git-Spigot-758abbe-2b00831]
    I got the code from here by the way: https://www.spigotmc.org/threads/need-help-with-setting-custom-armor-attributes.394694/

    Edit: SOLVED. I updated my Spigot and while the code I wrote in this post still doesn't work, another way I was trying and that was throwing errors too does. The working code is the following.

    Code:
    helmetMeta.addAttributeModifier(Attribute.GENERIC_ARMOR, new AttributeModifier("generic.armor", 2, Operation.ADD_NUMBER));
     
    Last edited: Jul 24, 2020

Share This Page