NullPointerException in PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by erikgamer07, Feb 9, 2018.

  1. Hello! Im making a Staff plugin and I have a PlayerInteractEvent in ClickListener class.

    This class returns NullPointerException and I don't know the reason.

    Error:

    https://ibb.co/bzuK0c

    Code:

    Code (Text):
        InspectMenu im;
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if(Main.frozen.get(p) != null) {
                e.setCancelled(true);
            }
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
               
                im = new InspectMenu();
               
                if(Main.toggled.get(p) == true) {
                    if(p.getItemInHand().getType() == Material.NETHER_STAR) {
                        Vector vector = p.getLocation().getDirection();
                        vector = vector.multiply(5);
                        p.setVelocity(vector);
                    }
                    if(p.getItemInHand().getType() == Material.SLIME_BALL) {
                        ArrayList<Player> player = new ArrayList<Player>();
                        for (Player e2 : Bukkit.getOnlinePlayers()) player.add(e2);
                        Player randomPlayer = player.get(new Random().nextInt(player.size()));
                        p.teleport(randomPlayer.getLocation());
                        p.sendMessage(chatprefix + ChatAPI.cc("&7Has Sido Teletransportado Aleatoriamente A &6" + randomPlayer.getName()));
                        player.clear();
                    }
                    if(p.getItemInHand().getType() == Material.PACKED_ICE) {
                       
                       
                        Bukkit.dispatchCommand(e.getPlayer(), "freeze " + im.targetPlayer.getName());
                    }
                    if(p.getItemInHand().getType() == Material.SUGAR) {
                        Bukkit.dispatchCommand(e.getPlayer(), "flymenu");
                    }
                   
                } else {
                    return;
                }
            }
              }


    You can help me?
    Thanks :3


    Is very important.

    Why return NullPointerException and how to fix it?
     
  2. because you're trying to invoke a method against null.
     
  3. Bold... bold... bold... my eyes... ;-;
    Also copy your stacktrace and put it in code tags/hastebin next time. ;)
     
    • Informative Informative x 1
  4. But, how I fix it?
     
  5. the followings could be null:

    • Main
    • Main.frozen
    • Main.toggled
    • im.targetPlayer
    my guess is im.targetPlayer is null since you instantiated InspectMenu but never set the targetPlayer.
     
  6. Assuming you didn't change your code since you post the error, what is line 53 in your ClickListener class?
     
  7. you don't really need to know what's on the line 53 in this case since there are only four things to see and rather obvious which one.
     
  8. If you look at the log, line 53 is causing the nullpointerexception. It might be the im.targetPlayer, but just to make sure.
     
  9. yes, but his code posted does not have line numbers but my point is you don't need to know since the source of the error is obvious.
     

Share This Page