Solved PlayerInteractEvent and NullPointerException

Discussion in 'Plugin Development' started by alf980103, May 19, 2013.

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

    alf980103

    Hi all, first of all thanks for taking the time to read this.
    I have a problem, and I don´t know how to fix it really, i have spend lots of time looking for a solution and all the things i tested fails.
    When a player has a specific item in his hand, and is sneaking an event should occur, it is curious that this event happens but gives errors in the console craftbukkit

    I show a portion of my code:

    Code:
    package DM;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapelessRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
     
    public final class DM extends JavaPlugin implements Listener {
     
        Logger logger = Logger.getLogger("Minecraft");
          @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            PluginDescriptionFile pdffile = getDescription();
            this.logger
                    .info(pdffile.getName() + ChatColor.WHITE
                            + " Han sido habilitadas." + "Version: "
                            + pdffile.getVersion());
        }
          @Override
        public void onDisable() {
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info(pdffile.getName() + ChatColor.WHITE
                    + " Han sido deshabilitadas!.");
        }
     
    //EVENT that fails :(
    // EVENTO MARIHUANA
    @EventHandler(priority = EventPriority.HIGHEST)
    public void EventoMarihuana(PlayerInteractEvent event){
    Player player = event.getPlayer();
    ItemStack item = player.getItemInHand();
    if (item.hasItemMeta()) {
    if (item.getItemMeta().getDisplayName()
    .equals("§a§lPorro de Marihuana")) {
    List<String> loreData = new ArrayList<String>();
    loreData.add("§eDroga");
    if (item.getItemMeta().hasLore()) {
    if (event.getItem().getType() == Material.INK_SACK)
    if( (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_BLOCK))) {
     
     
    if (player.isSneaking()) {
    if (item.getItemMeta().getLore()
    .equals(loreData)) {
    player.sendMessage("§eHas consumido Marihuana!");
    player.addPotionEffect(new PotionEffect(
    PotionEffectType.SLOW, 3600, 4));
    player.addPotionEffect(new PotionEffect(
    PotionEffectType.BLINDNESS, 300, 8));
    int amt = player.getItemInHand()
    .getAmount();
    if (amt > 1)
    player.getItemInHand().setAmount(
    amt - 1);
    else
    player.getInventory().setItemInHand(
    null);
    }
     
    }
    }
    }
    }
     
    }
     
    }
    
    And the log of the console:

    Code:
    >03:40:57 [SEVERE] Could not pass event PlayerInteractEvent to DrogasManiacas v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:186)
    at net.minecraft.server.v1_5_R3.PlayerInteractManager.interact(PlayerInteractManager.java:370)
    at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:631)
    at net.minecraft.server.v1_5_R3.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
    at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at DM.DM.EventoMarihuana(DM.java:196)
    at sun.reflect.GeneratedMethodAccessor240.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more
    
    Please tell me how to fix it,a cat will reward you if you help me :D
    Greetings
     
  2. Offline

    Garris0n

    It says at line 196 but I don't see a line 196 there...is that the full class?
     
  3. Offline

    ZeusAllMighty11

    You aren't checking if item in hand is air (null) so when you check if the player has item meta in hand but not an item, NPE.

    Also

    Code:
    eventomarihuana
    
     
  4. Offline

    alf980103

    I tried putting this:

    Code:
    if (item.getItemMeta().hasLore()) {
    if (player.getItemInHand().getType() == Material.AIR || player.getItemInHand() == null) {
    }
    if (event.getItem().getType() == Material.INK_SACK)
    if( (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_BLOCK))) {
     
    
    but stills doesn´t work :(
     
  5. Offline

    ZeusAllMighty11

    You are checking if a possibly null item has item lore, and item meta and item can both be null. Common sense
     
  6. Offline

    Minnymin3

    Do this: if (event.getItem().getType() != Material.AIR && event.getItem().hasItemMeta() && event.getItem().getType == Material.WHATEVER && event.getItem().getItemMeta().hasLore() && whatever you want to check
     
  7. Offline

    alf980103

    thanks for response, but stills doesnt works D:
    can you tell me what i need to do exactly?
    Code:
        // EVENTO MARIHUANA
        @EventHandler(priority = EventPriority.HIGHEST)
        public void eventomarihuana(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            ItemStack item = player.getItemInHand();
     
                    if (event.getItem().getType() != Material.AIR
                            && event.getItem().hasItemMeta()
                            && event.getItem().getType() == Material.INK_SACK
                            && event.getItem().getItemMeta().hasLore())
                        event.getAction().equals(Action.RIGHT_CLICK_BLOCK);
                    event.getAction().equals(Action.LEFT_CLICK_BLOCK);
                    {
                        if (item.hasItemMeta()) {
                            if (item.getItemMeta().getDisplayName()
                                    .equals("§a§lPorro de Marihuana"));
                                List<String> loreData = new ArrayList<String>();
                                loreData.add("§eDroga");
                        if (player.isSneaking()) {
                            if (item.getItemMeta().getLore().equals(loreData)) {
                                player.sendMessage("§eHas consumido Marihuana!");
                                player.addPotionEffect(new PotionEffect(
                                        PotionEffectType.SLOW, 3600, 4));
                                player.addPotionEffect(new PotionEffect(
                                        PotionEffectType.BLINDNESS, 300, 8));
                                int amt = player.getItemInHand().getAmount();
                                if (amt > 1)
                                    player.getItemInHand().setAmount(amt - 1);
                                else
                                    player.getInventory().setItemInHand(null);
                            }
     
                        }
     
                    }
                }
     
        }
     
  8. Offline

    alf980103

  9. Offline

    marwzoor

    Well you have to check if the item isn't null first!
    Because sometimes if the item is air then it is null.

    Code:java
    1.  
    2. if(event.getItem() != null)
    3. {
    4. //check if the material isnt air and do your stuffz
    5. }
    6.  
     
    lenis0012 likes this.
  10. Offline

    Zachster

    You could also put in try/catch with an empty catch.
     
  11. Zachster
    You shouldn't do that, it's slow and allows a problem to go unfixed which can cause other problems.
     
  12. Offline

    alf980103

    I feel stupid...
    btw thanks to everybody support, have fixed :D
     
  13. Offline

    Smooshman12345

    im new to programing but where do i find the lines of code? thanks :D
     
Thread Status:
Not open for further replies.

Share This Page