Solved How come it returns to me null

Discussion in 'Plugin Development' started by mAndAle, Jul 4, 2021.

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


    Hi everyone, I need a hand in understanding why this code returns a nullPointer even though it doesn't seem like it can

    this the errror:

    Caused by: java.lang.NullPointerException
            at ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_292]
            at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_292]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_292]
            at java.lang.reflect.Method.invoke( ~[?:1.8.0_292]
            at$1.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24
    and this is my class:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.inventory.ItemStack;
    import org.salex.pvpsword.Main;
    public class SwordEvent implements Listener {
        private Main main;
        private List<UUID> pvp;
        private ItemStack[] armor;
        private ItemStack[] armor1;
        public SwordEvent(Main m) {
            main = m;
            this.pvp = new ArrayList<UUID>();
            this.armor = new ItemStack[4];
            this.armor1 = new ItemStack[4];
        public void onCambioItem(PlayerItemHeldEvent event) {
            Player p = event.getPlayer();
            armor[3] = (new ItemStack(Material.DIAMOND_HELMET));
            armor[2] = (new ItemStack(Material.DIAMOND_CHESTPLATE));
            armor[1] = (new ItemStack(Material.DIAMOND_LEGGINGS));
            armor[0] = (new ItemStack(Material.DIAMOND_BOOTS));
            String nome = main.getConfig().getString("");
            if(p.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals(nome) && !pvp.contains(p.getUniqueId())) {
            } else if (!p.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals(nome)  && pvp.contains(p.getUniqueId())) {
        public List<UUID> getPvp() {
            return pvp;
        public ItemStack[] getArmor() {
            return armor;
    anyone know why?
  2. Offline


    It's because Player#getItemInHand() returns null if they aren't holding anything, and so it causes an error because it can't get attributes such as item meta. It's simple to combat this, just add:
    ItemStack item = //Player#getItemInHand();
    if (item != null && !item.getType().equals(Material.AIR)) {
Thread Status:
Not open for further replies.

Share This Page