Solved Nullpointer right click with item?

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

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

    robbertie

    Hello guys,

    I have made a TeamDM plugin but I have one problem.
    But when I right click with a item that is not the one that I registered it gives met this error:

    26.11 18:28:51 [Server] INFO Caused by: java.lang.NullPointerException

    this is my code:
    Code:java
    1. else if(e.getAction() == Action.RIGHT_CLICK_BLOCK ){
    2. if(player.getItemInHand().getType() == Material.AIR){
    3. return;
    4. }if(player.getItemInHand().getType().isBlock()){
    5. return;
    6. }
    7.  
    8. if(player.getItemInHand().getItemMeta().getDisplayName().contains("Kit Selector")){
    9. inventory.clear();
    10. inventory.setItem(0, hn.Warrior());
    11. inventory.setItem(1, hn.Archer());
    12. inventory.setItem(2, hn.Tank());
    13. inventory.setItem(3, hn.Scout());
    14. inventory.setItem(4, hn.Assassin());
    15. player.openInventory(inventory);
    16. }else if(player.getItemInHand().getItemMeta().getDisplayName().contains("Apple of health")){
    17. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 2, 0));
    18.  
    19. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    20. public void run() {
    21. player.setItemInHand(new ItemStack(Material.AIR));
    22. }
    23. }, 1L);
    24.  
    25. player.sendMessage(ChatColor.GREEN + "Je bent gehealed!");
    26. }else if(player.getItemInHand().getItemMeta().getDisplayName().contains("Carrot of Invisibility")){
    27. if(TeamDM.inv.contains(player.getName())){
    28. player.sendMessage(ChatColor.RED + "Je bent al onzichtbaar!");
    29. return;
    30. }
    31. if(TeamDM.cd.contains(player.getName())){
    32. player.sendMessage(ChatColor.RED + "De cooldown is nog steeds bezig!");
    33. return;
    34. }else{
    35. if(TeamDM.dead.contains(player.getName())) return;
    36. TeamDM.cd.add(player.getName());
    37. TeamDM.inv.add(player.getName());
    38. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 0));
    39. player.getInventory().setArmorContents(null);
    40. player.sendMessage(ChatColor.GREEN + "Je bent nu onzichtbaar!");
    41. player.getInventory().setArmorContents(null);
    42. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    43. public void run() {
    44. if(!TeamDM.inv.contains(player.getName())) return;
    45. TeamDM.inv.remove(player.getName());
    46. if(TeamDM.dead.contains(player.getName())) return;
    47. player.removePotionEffect(PotionEffectType.INVISIBILITY);
    48. player.getInventory().setHelmet(hn.AssassinHat());
    49. player.sendMessage(ChatColor.RED + "Je bent niet meer onzichtbaar!");
    50. }
    51. }, 200L);
    52. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    53. public void run() {
    54. if(!TeamDM.cd.contains(player.getName())) return;
    55. TeamDM.cd.remove(player.getName());
    56. player.sendMessage(ChatColor.GREEN + "Je kan weer onzichtbaar worden!");
    57. }
    58. }, 400L);
    59. }
    60. }
    61. }
    62.  
    63. }


    Some things are dutch though.
     
  2. Which line?
     
  3. Offline

    robbertie

    DreTaX thanks for fast response! On this line :
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().contains("Kit Selector")){
     
  4. Offline

    sgavster

    PHP:
    if(player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().hasDisplayName()) {
    //your code
    }
    It's because something is null, implies the nullpointerexception.
     
  5. Offline

    robbertie

    sgavster ok thanks I will try that!
     
  6. Offline

    Garris0n

    -If the player has no item in their hand the getItemInHand() will return null.
    -If the item has no meta the getItemMeta() will return null.
    -If the item has no display name the getDisplayName will return null.

    You have to check
    -item != null
    -item.hasItemMeta()
    -meta.hasDisplayName()
     
  7. Offline

    robbertie

    Thanks that worked! sgavster and Garris0n , maybe a little bit off topic but how do you stop a syncdelayedtask for one player?
     
  8. Offline

    Garris0n

    When you register it, it returns an integer that's its id. You can do Bukkit.getServer().getScheduler().cancelTask(id).
     
  9. Offline

    sgavster

  10. Offline

    robbertie

  11. Offline

    Garris0n

    Code:java
    1. int id = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    2. public void run() {
    3. //scheduler stuff
    4. }
    5. }, 1L);
    6.  
    7. //whenever you want to cancel it...
    8. Bukkit.getServer().getScheduler().cancelTask(id);
    9.  


    If you wanted to save them per-player, you could put them in a HashMap with the player's name(NEVER store player instances). Note that this system is pretty much only useful when you're using repeating tasks, though there are exceptions.
     
  12. Offline

    robbertie

Thread Status:
Not open for further replies.

Share This Page