PlayerInteractEvent

Discussion in 'Plugin Development' started by CraftRay, Mar 25, 2016.

Thread Status:
Not open for further replies.
  1. How to close mistake:
    Code:
    Caused by: java.lang.NullPointerException
            at me.xcraftrayx.pl.Button.buttonClick(Button.java:34)
    This mistake show me when I clicked block and is cancel this event.

    My code:
    Code:
    package me.xcraftrayx.pl;
    
    import java.util.HashMap;
    
    import javax.persistence.GenerationType;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Button extends JavaPlugin implements Listener {
       
        public void onEnable()
        {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
        HashMap<Player, Integer> Mapa = new HashMap<>();
       
        Location Loc1 = new Location(Bukkit.getWorld("world2"), 46, 53, -3);
        Location Loc2 = new Location(Bukkit.getWorld("world2"), 44, 55, 15);
        Location Loc3 = new Location(Bukkit.getWorld("world2"), 39, 54, 16);
        Location Loc4 = new Location(Bukkit.getWorld("world2"), 37, 56, 2);
        Location Loc5 = new Location(Bukkit.getWorld("world2"), 41, 55, -1);
       
        @EventHandler
        public void buttonClick(PlayerInteractEvent e)
        {
            if(e.getClickedBlock().getType() == Material.STONE_BUTTON)
            {
                double x = e.getClickedBlock().getLocation().getX();
                double y = e.getClickedBlock().getLocation().getY();
                double z = e.getClickedBlock().getLocation().getZ();
           
                Location loc = new Location(Bukkit.getWorld("world2"), x, y, z);
                Player p = e.getPlayer();
                    if(loc.equals(Loc1))
                    {
                        Mapa.put(p, 1);
                        p.sendMessage("To jest 1 guzik");
                        System.out.println(Mapa.get(p));
                    }
           
                    if(loc.equals(Loc2))
                    {
                        if(Mapa.containsKey(p))
                        {
                            if(Mapa.get(p).equals(1))
                            {
                                Mapa.put(p, 2);
                                p.sendMessage("To jest 2 guzik");
                                System.out.println(Mapa.get(p));
                            }
                            else{
                                Mapa.remove(p);
                            }
                        }
                    }
           
                    if(loc.equals(Loc3))
                    {
                        if(Mapa.containsKey(p))
                        {
                            if(Mapa.get(p).equals(2))
                            {
                                Mapa.put(p, 3);
                                p.sendMessage("To jest 3 guzik");
                                System.out.println(Mapa.get(p));
                            }
                            else{
                                Mapa.remove(p);
                            }
                        }
                    }
           
                    if(loc.equals(Loc4))
                    {
                        if(Mapa.containsKey(p))
                        {
                            if(Mapa.get(p).equals(3))
                            {
                                Mapa.remove(p);
                                p.sendMessage("To jest 4 guzik");
                                p.sendMessage("Zostaniesz wynagrodzony!");
                            }
                            else{
                                Mapa.remove(p);
                            }
                        }
                    }
            }
        }
    
    }
    
     
  2. Basically, NullPointerException means one of your values has returned null. In this instance, it should say something like (Java:##) to show the exact line with the error. This is how you bug-fix NullPointerException. Anyway, here you have attempted to identify a block type with a Material. This is an error because Material is not Block.

    Line 34, the if statement
     
  3. Offline

    Ligachamp

    As Harieo said, check that if statement. Use .equals to compare objects instead of "==".
     
  4. @Ligachamp When i ussed "==" problem don't close...
    @Harieo This mistake pops me when I hold item in hand and clicked block under me, so when I haven't item in hand no mistake... how to solve it?
     
  5. Offline

    WolfMage1

    == is fine on Enum's

    @CraftRay It causes the NPE because you're interacting with the item in hand, not the block,(I'm guessing) check if they clicked a block first before going through with everything else
     
  6. I believe it is an eventhandler... So I don't see why you have discounted my answer?
    It may also be that when you place a block, the interact suddenly changes?

    Try making sure iteminhand is nothing as a safeguard
     
  7. Offline

    Ligachamp

    Oh, you're right. Didn't think of that ^^"

    Isn't the metod to get the actual material ".getMaterial()" instead of ".getType()"?

    After all, what about "world2"? Does it even exist?
     
  8. Offline

    mcdorli

    getType() is correct
     
    CraftRay likes this.
  9. Offline

    Zombie_Striker

    @CraftRay
    It you who has to mark this thread as solveD:

    Go to the top of this page> look for a "thread tools" button, click edit title, and add the solved prefix.
     
Thread Status:
Not open for further replies.

Share This Page