Checking if entity is player when creating portal returning null

Discussion in 'Plugin Development' started by Minesuchtiiii, Jun 16, 2019.

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

    Minesuchtiiii

    I'm checking if an entity which creates a portal is a player, but this always returns null, I'm going crazy

    Code:
        @EventHandler
        public void onRcPortalCheck(PortalCreateEvent e) {
    
    
            if(e.getEntity() instanceof Player) {
           
                Player p = (Player) e.getEntity();
                Bukkit.broadcastMessage("Done");
                if(this.plugin.portals.containsKey(p.getUniqueId())) {
                   
                    Bukkit.broadcastMessage("done 2");
                    this.plugin.portals.put(p.getUniqueId(), this.plugin.portals.get(p.getUniqueId()) + 1);
                   
                }
                else {
                   
                    Bukkit.broadcastMessage("not in the list");
                   
                }
       
        }
            else {
               
                Bukkit.broadcastMessage("Not a player");
               
            }
        }
    Event is registered in the main class!
     
  2. Offline

    KarimAKL

    @Minesuchtiiii Where exactly do you get null?
    EDIT: I'm not sure about this but maybe 'PortalCreateEvent#getEntity()' could return null.
     
  3. Offline

    Minesuchtiiii

    returning null at e.getEntity() instanceof Player
     
  4. Offline

    KarimAKL

    @Minesuchtiiii At that line or inside that scope? If it's at that line then i have no clue, doing "Object instanceof Class" should just return false if the object is null.
     
  5. Offline

    Minesuchtiiii

    In that close, I always use this method to check if the entity is a player before I cast, in other events it works just fine
     
  6. Offline

    KarimAKL

    @Minesuchtiiii I see, it makes sense now. :p In that case i think 'plugin' or 'portals' (plugin.portals) is null.
     
  7. Offline

    Minesuchtiiii

    I debugged those and it's nothing wrong with them. I start thinking that .getEntity() in the event returns null no matter what
     
  8. Offline

    Kars

    Try this:
    Code:
    Block block = e.getBlocks().get(0);
    Player p = ExtractPlayerFromLocation(block.getLocation());
     
  9. Offline

    KarimAKL

    @Minesuchtiiii But if that was the case, you would get the message "Not a player" all the time.
    EDIT: Do you get a NullPointerException or is something simply null? What debug messages do you get?
     
  10. Offline

    Minesuchtiiii

    It's just a "null" error message.

    I'll give it a try
     
  11. Offline

    KarimAKL

    Where do you print the message that says "null"?
     
  12. Offline

    Minesuchtiiii

    It's in the console, not my debug message but the standart stacktrace
     
  13. Offline

    KarimAKL

  14. Offline

    Minesuchtiiii

    I removed the code as I decided not to use it, but it was a NullPointerException at the line where I check if the entity is a player
     
  15. Offline

    KarimAKL

    @Minesuchtiiii I see, but i wanted to see the rest of the stacktrace because it might show what exactly was causing the NullPointerException.
     
Thread Status:
Not open for further replies.

Share This Page