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

    mAndAle

    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:

    Code:
    Caused by: java.lang.NullPointerException
            at org.salex.pvpsword.events.SwordEvent.onCambioItem(SwordEvent.java:41) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_292]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_292]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_292]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_292]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24
    and this is my class:

    Code:
    package org.salex.pvpsword.events;
    
    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];
        }
       
       
        @EventHandler
        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("sword.name");
           
            if(p.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals(nome) && !pvp.contains(p.getUniqueId())) {
                p.getInventory().setArmorContents(armor);
                pvp.add(p.getUniqueId());
            } else if (!p.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals(nome)  && pvp.contains(p.getUniqueId())) {
                p.getInventory().setArmorContents(armor1);
                pvp.remove(p.getUniqueId());
            }
        }
       
        public List<UUID> getPvp() {
            return pvp;
        }
       
        public ItemStack[] getArmor() {
            return armor;
        }
       
    }
    
    anyone know why?
     
  2. Offline

    c7dev

    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:
    Code:
    ItemStack item = //Player#getItemInHand();
    if (item != null && !item.getType().equals(Material.AIR)) {
        //code
    }
     
Thread Status:
Not open for further replies.

Share This Page