Solved Right click block nullpointer error

Discussion in 'Plugin Development' started by robbertie, Nov 22, 2013.

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

    robbertie

    Hello guys,

    I got a nullpointer exception when I right click on a block with nothing in my hand..
    My code is:
    Code:java
    1. if(e.getAction() == Action.RIGHT_CLICK_BLOCK ){
    2. if(player.getItemInHand() == null) {
    3.  
    4. }
    5. else if(!player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Hub") && !player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Kit Selector") && !player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "Apple of health")){
    6.  
    7. }
    8. else if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + "Hub")){
    9. if(block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN){
    10. Sign sign = (Sign) e.getClickedBlock().getState();
    11. if(sign.getLine(0).contains(ChatColor.GREEN + "TeamDM")){
    12. return;
    13. }else if(block.getType() != Material.SIGN_POST || block.getType() != Material.WALL_SIGN){
    14. inventoryh.clear();
    15. inventoryh.setItem(0, hn.Spawn());
    16. inventoryh.setItem(1, hn.TeamDM());
    17. player.openInventory(inventoryh);
    18. }
    19. }
    20. }else if(player.getItemInHand().getItemMeta().getDisplayName().contains("Kit Selector")){
    21. inventory.clear();
    22. inventory.setItem(2, hn.Warrior());
    23. inventory.setItem(4, hn.Archer());
    24. inventory.setItem(6, hn.Tank());
    25. player.openInventory(inventory);
    26. }else if(player.getItemInHand().getItemMeta().getDisplayName().contains("Apple of health")){
    27. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 2 ,0));
    28.  
    29. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    30. public void run() {
    31. player.setItemInHand(new ItemStack(Material.AIR));
    32. }
    33. }, 1L);
    34.  
    35. player.sendMessage(ChatColor.GREEN + "Je bent gehealed!");
    36. }else{
    37. return;
    38. }
    39. }


    Please help :D
     
  2. Offline

    boysnnoco

    Error log where is the nullpointer getting throw at? @robberti
     
  3. Offline

    robbertie

    I think I already fixed it boysnnoco

    But I got the nullpointer on this line
    Code:java
    1. else if(!player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Hub") && !player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Kit Selector") && !player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "Apple of health")){
    2.  
    3. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    1Rogue

    you need to have a check if the itemInHand is null, and if it even has item meta with .hasItemMeta() (on the item stack) before you attempt getting it.
     
  5. Offline

    GusGold

    Mark thread as solved then?
     
Thread Status:
Not open for further replies.

Share This Page