Solved What is this error?

Discussion in 'Plugin Development' started by DrTURTLE2, Aug 18, 2013.

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

    DrTURTLE2

  2. Offline

    Lolmewn

    Line 167 has something that is null
     
  3. Offline

    DrTURTLE2

    Code:java
    1. if (e.getItem().getType() == Material.IRON_SPADE && e.getAction() == Action.RIGHT_CLICK_AIR) {


    Thats the code..
     
  4. Offline

    dark navi


    Depending on the context, it is very possible for e.getItem() to be null.
     
  5. Offline

    DrTURTLE2


    Soo... Should I post the whole event that this line is in?
     
  6. Offline

    dark navi


    Sure, post the entire event handler.
     
  7. Offline

    DrTURTLE2

    dark navi
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. final Player player = (Player) e.getPlayer();
    4. if (e.getItem().getType() == Material.IRON_SPADE && e.getAction() == Action.RIGHT_CLICK_AIR) {
    5. if (players.contains(player.getName())) {
    6. player.sendMessage(ChatColor.RED + "You are still on cooldown!");
    7. } else {
    8. players.add(player.getName());
    9. new BukkitRunnable() {
    10. public void run() {
    11. players.remove(player.getName());
    12. player.sendMessage(ChatColor.GREEN + "You can now shoot again!");
    13. }
    14. }.runTaskLater(this, 60);
    15. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    16. f.setIsIncendiary(false);
    17. f.setYield(0);
    18.  
    19. }
     
  8. Offline

    epicfacecreeper

    Make sure the item is not null.
     
  9. Offline

    DrTURTLE2


    How would it be null?
     
  10. Offline

    dark navi


    If you look here, you'll see that the PlayerInteractEvent covers a pretty wide range of events. For example, if you step on a pressure plate, getItem() doesn't really make sense. As well as if you right click with no item in your hand. I'd suggest checking the event type first and then checking to see if the item is valid.
     
  11. Offline

    DrTURTLE2

    dark navi
    The thing is the plugin runs smoothly, I just don't like getting the console spammed.
     
  12. Offline

    Compressions

    DrTURTLE2 If the player is not holding an item in their hand, getItem() will return null.
     
  13. Offline

    xTrollxDudex

  14. Offline

    DrTURTLE2


    How?
     
  15. Offline

    epicfacecreeper

    GABALFALOLAZA

    e.getItem() == null
     
    xTrollxDudex likes this.
  16. Offline

    DrTURTLE2

    Ik that, but where.
     
  17. Offline

    epicfacecreeper

    *deep breath*

    In the if statement before the getItem() == IRON_SPADE bit.
     
    DrTURTLE2 likes this.
  18. Offline

    DrTURTLE2

    Sorry D:

    Thanks btw

    Wait now its not even shooting.

    epicfacecreeper

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

    epicfacecreeper

    Full event code please.
     
  20. Offline

    DrTURTLE2

    epicfacecreeper
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. final Player player = (Player) e.getPlayer();
    4. if (e.getItem() == null)
    5. if (e.getItem().getType() == Material.IRON_SPADE && e.getAction() == Action.RIGHT_CLICK_AIR) {
    6. if (players.contains(player.getName())) {
    7. player.sendMessage(ChatColor.RED + "You are still on cooldown!");
    8. } else {
    9. players.add(player.getName());
    10. new BukkitRunnable() {
    11. public void run() {
    12. players.remove(player.getName());
    13. player.sendMessage(ChatColor.GREEN + "You can now shoot again!");
    14. }
    15. }.runTaskLater(this, 60);
    16. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    17. f.setIsIncendiary(false);
    18. f.setYield(0);
    19.  
    20. }
    21. }
    22. }
     
  21. use e.getItem() != null as you only want it to work when e.getItem() is not null
     
  22. Offline

    DrTURTLE2

    Thanks works hapm
     
Thread Status:
Not open for further replies.

Share This Page