NullPointerException when punching air

Discussion in 'Plugin Development' started by quasar098, Mar 13, 2021.

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

    quasar098

    I keep getting NullPointerException when punching while holding nothing.

    if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
    if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Booster")) { // ERROR HERE ON 2nd LINE
    if (player.getInventory().containsAtLeast(ThirtyTwoWool, 32)) {
    quickBits.removeItems(Material.WOOL, 32, player);
    Vector launch = player.getLocation().getDirection();
    launch.multiply(1.4);
    launch.setY(2);
    player.setVelocity(launch);
    }

    It is a PlayerInteractEvent btw. Ive tried a lot of things like canceling the event if the player is holding null, but nothing seems to work.
     
  2. Offline

    KarimAKL

    @quasar098
    Code:Java
    1. if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Booster")) {

    Two things can cause the NullPointerException here:
    • getItemMeta().getDisplayName()
    • getDisplayName().equals(ChatColor.WHITE + "Booster")
    The ItemMeta can be null, or the display name can be null. You should not try to compress all of this into a single line. There is no problem with separating it, in fact, it is even preferred because it increases readability.
     
    Strahan likes this.
  3. Offline

    quasar098

Thread Status:
Not open for further replies.

Share This Page