Solved NPE But not sure why?

Discussion in 'Plugin Help/Development/Requests' started by callum2904, Jan 24, 2015.

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

    callum2904

    Code:
    [00:33:16 ERROR]: Could not pass event EntityDamageByEntityEvent to Roleplay v0.
    1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303) ~[spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:509) [spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:494) [spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callEvent(Craf
    tEventFactory.java:85) [spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callEntityDama
    geEvent(CraftEventFactory.java:538) [spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.handleEntityDa
    mageEvent(CraftEventFactory.java:451) [spigot.jar:git-Spigot-"606148f"]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.handleLivingEn
    tityDamageEvent(CraftEventFactory.java:570) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.EntityLiving.d(EntityLiving.java:1055) [
    spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.EntityLiving.damageEntity(EntityLiving.j
    ava:705) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.EntityMonster.damageEntity(EntityMonster
    .java:44) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.EntityZombie.damageEntity(EntityZombie.j
    ava:168) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.EntityHuman.attack(EntityHuman.java:1000
    ) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.EntityPlayer.attack(EntityPlayer.java:10
    43) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :1303) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.PacketPlayInUseEntity.a(SourceFile:52) [
    spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.PacketPlayInUseEntity.a(SourceFile:11) [
    spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spi
    got.jar:git-Spigot-"606148f"]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_20]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_20]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    84) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    25) [spigot.jar:git-Spigot-"606148f"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :528) [spigot.jar:git-Spigot-"606148f"]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_20]
    Caused by: java.lang.NullPointerException
            at me.callum2904.Roleplay.Items.ItemListener.getWeaponAttributes(ItemLis
    tener.java:34) ~[?:?]
            at me.callum2904.Roleplay.Items.ItemListener.getAttribute(ItemListener.j
    ava:23) ~[?:?]
            at me.callum2904.Roleplay.Items.ItemListener.incorrectClass(ItemListener
    .java:56) ~[?:?]
            at me.callum2904.Roleplay.Items.ItemListener.onDambyEnt(ItemListener.jav
    a:72) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _20]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _20]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_20]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_20]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:299) ~[spigot.jar:git-Spigot-"606148f"]
            ... 24 more
    
    
    There is my error and here is my code...
    Code:
    import java.util.HashMap;
    
    import me.callum2904.Roleplay.Objects.RPAttribute;
    import me.callum2904.Roleplay.Objects.RPClass;
    import me.callum2904.Roleplay.Objects.RPUser;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ItemListener implements Listener {
    
        public Object getAttribute(Player player, RPAttribute att) {
            ItemStack item = player.getItemInHand();
            if (!item.getType().equals(Material.AIR) || !item.getType().equals(null)) {
                HashMap<RPAttribute, Object> atts = getWeaponAttributes(item);
                if (atts.containsKey(att)) {
                    return atts.get(att);
                }
            }
            return null;
        }
    
        public HashMap<RPAttribute, Object> getWeaponAttributes(ItemStack item) {
            HashMap<RPAttribute, Object> hm = new HashMap<RPAttribute, Object>();
            if (!item.getType().equals(Material.AIR) || !item.getType().equals(null)) {
                if (item.getItemMeta().hasLore()) {
                    for (String lore : item.getItemMeta().getLore()) {
                        for (RPAttribute attr : RPAttribute.values()) {
                            String at = attr.getDesc().replace("<value>", "");
                            if (lore.contains(at)) {
                                Object value = lore.replace(at, "");
                                if (at.contains("Class Type:")) {
                                    RPClass rpclass = RPClass.getRPClass(value.toString());
                                    hm.put(attr, rpclass);
                                    continue;
                                }
                                hm.put(attr, value);
                            }
                        }
                    }
                }
            }
            return hm;
        }
    
        public boolean incorrectClass(Player player) {
            RPUser user = new RPUser(player.getUniqueId());
            RPClass rank = (RPClass) getAttribute(player, RPAttribute.CLASS_TYPE);
            if (!user.getRPClass().equals(rank))
                return true;
            return false;
        }
    
        @EventHandler
        public void onAction(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            event.setCancelled(incorrectClass(player));
        }
    
        @EventHandler
        public void onDambyEnt(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player) {
                Player player = (Player) event.getDamager();
                event.setCancelled(incorrectClass(player));
            }
        }
    
        @EventHandler
        public void onDam(EntityDamageEvent event) {
            if (event.getEntity() instanceof Player) {
                Player player = (Player) event.getEntity();
                if (player.isBlocking()) {
                    event.setCancelled(incorrectClass(player));
                }
            }
        }
    }
    I know the error is on line 34 which is:
    Code:
    if (item.getItemMeta().hasLore()) {
     
  2. Offline

    Peavalu

    Hello, the way I'm reading this, it appears that the error is caused on another line.

    Code:
            at me.callum2904.Roleplay.Items.ItemListener.getWeaponAttributes(ItemLis
    tener.java:34) ~[?:?]
            at me.callum2904.Roleplay.Items.ItemListener.getAttribute(ItemListener.j
    ava:23) ~[?:?]
            at me.callum2904.Roleplay.Items.ItemListener.incorrectClass(ItemListener
    .java:56) ~[?:?]
            at me.callum2904.Roleplay.Items.ItemListener.onDambyEnt(ItemListener.jav
    a:72) ~[?:?]
    Code:
     @EventHandler
        public void onDambyEnt(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player) {
                Player player = (Player) event.getDamager();
                event.setCancelled(incorrectClass(player));
            }
        }

    Starting at line 72:
    Code:
     event.setCancelled(incorrectClass(player));
    seems to be causing the error.
    (Correct me if I'm wrong)
    It seems that getWeaponAttributes is acting up because of something. If you would provide the code for your incorrectClass maybe someone here could help.

    Cheers'
    Peavalu
     
  3. Offline

    callum2904

    incorrectClass is in the itemlistener i put up but here it is again
    Code:
    public boolean incorrectClass(Player player) {
            RPUser user = new RPUser(player.getUniqueId());
            RPClass rank = (RPClass) getAttribute(player, RPAttribute.CLASS_TYPE);
            if (!user.getRPClass().equals(rank))
                return true;
            return false;
        }
    
    I should probably change that method name it sounds like it is a class file not a roleplay class
    Other classes that may be involved are here:
    Code:
    public enum RPAttribute {
        MIN_LEVEL("Min. Level: <value>"), MAX_LEVEL("Max. Level: <value>"), DAMAGE("Damage: <value>"), DEFENCE(
                "Defence: <value>"), HEALTH_REGEN("Health Regen: <value>"), SPEED(
                "Speed Boost: <value>"), XP_BONUS("XP Bonus: <value>"), CLASS_TYPE(
                "Class Type: <value>");
    
        RPAttribute(String desc) {
            this.desc = desc;
        }
    
        String desc;
    
        public String getDesc() {
            return desc;
        }
    }
    Code:
    public enum RPClass {
    
        KNIGHT("Knight"), ARCHER("Archer"), MAGE("Mage"), ASSASSIN("Assassin"), UN_CHOSEN("UnSpecified");
    
        String tag;
    
        RPClass(String tag) {
            this.tag = tag;
        }
    
        public String getTag() {
            return this.tag;
        }
    
        public static RPClass getRPClass(String string) {
            for (RPClass i : RPClass.values()) {
                if (i.getTag().equalsIgnoreCase(string))
                    return i;
            }
            return null;
        }
    
    }
     
    Last edited by a moderator: Jan 24, 2015
  4. Offline

    adam753

    @callum2904
    getItemMeta() can return null, check item.hasItemMeta() first.
     
  5. Offline

    callum2904

    Yeah i just realized that just before you posted. Thanks for your reply and good spotting it just took me about 50 tries.
     
  6. Offline

    Experminator

    @callum2904 Problem solved? If yes, please mark this thread as Solved.
     
  7. Offline

    teej107

  8. Offline

    Konato_K

    @adam753 No, getItemMeta will never return null, if the item has no meta then it will create one for the item.
     
    WinX64 likes this.
  9. Offline

    Skionz

    Interesting, I didn't know that until now.
    Code:
    public ItemMeta getItemMeta() {
        return this.meta == null ? Bukkit.getItemFactory().getItemMeta(getType0()) : this.meta.clone();
    }
       
     
  10. Offline

    adam753

    @Konato_K @Skionz
    That is interesting. In that case, I'll have to ask @callum2904 to make sure that the error you've posted matches up with the code you've posted, because we both have the wrong line number here.
     
  11. Offline

    callum2904

    My internet browser does not allow me to mark it as solved at the moment, I click it and then the page just attempts to refresh over and over without actually marking it as done. I need a moderator to mark it as finished!

    For me it was returning null because I no longer get any NPE when i add ...hasItemMeta();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  12. Offline

    Experminator

    @callum2904 Okay. Then you could report this thread with 'Thread solved. Please mark the thread as Solved. and close it. (Because: ...)'
     
  13. Offline

    mrCookieSlime

    Moved to Alternatives Section.

    Marked as Solved. (On request)
     
Thread Status:
Not open for further replies.

Share This Page