Getting an NPE for a sign

Discussion in 'Plugin Development' started by GamerBah, Feb 1, 2015.

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

    GamerBah

    Hi there, I seem to be getting a NullPointer @ "Sign sign = (Sign) b.getState();"
    Code:
    @EventHandler
     
        public void onSignClick(PlayerInteractEvent e) {
     
            Player p = e.getPlayer();
     
            Block b = e.getClickedBlock();
     
            Sign sign = (Sign) b.getState();
     
            String gameworld = sign.getLine(0).toString();
     
            World w = Bukkit.getServer().getWorld(gameworld);
     
            Location l = new Location(w, 0, 5, 0);
     
            if (b.getType().equals(Material.SIGN_POST) || b.getType().equals(Material.WALL_SIGN)) {
     
                if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
     
                    if (p.getGameMode().equals(GameMode.CREATIVE)) {
     
                        p.teleport(l);
     
                        e.setCancelled(true);
     
                    } else {
     
                        p.teleport(l);
     
                        e.setCancelled(true);
     
                    }
     
                }
     
                if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
     
    
     
                }
     
            }
     
        }
    I'm probably too stupid to know why this is happening, so help is always appreciated :p
     
  2. Offline

    Permeer

    @GamerBah
    Code:
     Sign sign = (Sign) b.getState();
    Cast the state after you check the block type.
     
  3. Offline

    sirrus86

    getClickedBlock() can return null if the player clicks air, which will give you an NPE right after.

    And like @Permeer said, make sure the block type is a sign before casting it.
     
  4. Offline

    GamerBah

    @Permeer I changed my code to this:
    Code:
    @EventHandler
       public void onSignClick(PlayerInteractEvent e) {
         Player p = e.getPlayer();
         Block b = e.getClickedBlock();
         if (b.getType().equals(Material.SIGN_POST)
             || b.getType().equals(Material.WALL_SIGN)) {
           Sign sign = (Sign) b.getState();
           String gameworld = sign.getLine(0).toString();
           World w = Bukkit.getServer().getWorld(gameworld);
           Location l = new Location(w, 0, 5, 0);
           if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
             if (p.getGameMode().equals(GameMode.CREATIVE)) {
               p.teleport(l);
               e.setCancelled(true);
             } else {
               p.teleport(l);
               e.setCancelled(true);
             }
           }
           if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    
           }
         }
       }
    
    but now it's giving me an NPE at "if (b.getType().equals(Material.SIGN_POST)
    || b.getType().equals(Material.WALL_SIGN)) {"

    Should I do a check for air?
     
  5. Offline

    Konato_K

    Block is null, check if block is null
    @GamerBah
     
  6. Offline

    Permeer

  7. Offline

    GamerBah

    Changed it to this:
    Code:
    @EventHandler
        public void onSignClick(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Block b = e.getClickedBlock();
            if (b.equals(null)) {
                return;
            }
            if (!b.equals(null) || b.getType().equals(Material.AIR)) {
                if (b.getType().equals(Material.SIGN_POST)
                        || b.getType().equals(Material.WALL_SIGN)) {
                    Sign sign = (Sign) b.getState();
                    String gameworld = sign.getLine(0).toString();
                    World w = Bukkit.getServer().getWorld(gameworld);
                    Location l = new Location(w, 0, 5, 0);
                    if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                        if (p.getGameMode().equals(GameMode.CREATIVE)) {
                            p.teleport(l);
                            e.setCancelled(true);
                        } else {
                            p.teleport(l);
                            e.setCancelled(true);
                        }
                    }
                    if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    
                    }
                }
            }
        }
    Getting an NPE at "if (b.equals(null)) {"

    Really started to get confused...
     
  8. Offline

    sirrus86

    Try "if (b == null)" instead.
     
  9. Offline

    nverdier

    @GamerBah You can't invoke a method on a null reference, so you'd have to use the '==' operator.
     
Thread Status:
Not open for further replies.

Share This Page