NullPointer on Interact

Discussion in 'Plugin Development' started by travja, Nov 10, 2012.

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

    travja

    I've been trying to use the PlayerInteractEvent to get if they interacted with a button at the correct coordinates. However, I use Block b = event.getClickedBlock() and then later NPEs I use b.getBLAH and it returns NPE so I'm assuming it thinks that the block that I'm interacting with is null... And it does it whenever I interact with anything, even throwing potions.....
    Code (open)

    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent event){
    3. Block b = event.getClickedBlock();
    4. Player p = event.getPlayer();
    5. String pname = p.getName();
    6. //TODO Button location
    7. Location l = new Location(getServer().getWorld("Games"), -770, 5, -914);
    8. if(b.getLocation()== l){
    9. if(b.getTypeId()== 143 || b.getTypeId()== 77){
    10. if(!players.contains(pname)){
    11. if(!inGame){
    12. players.add(pname);
    13. getServer().broadcastMessage(ChatColor.GOLD + "[SnowBall] " + ChatColor.AQUA + pname + ChatColor.GREEN + " Joined the snowball fight, game starts in " + ChatColor.LIGHT_PURPLE + i + ChatColor.GREEN + " seconds!");
    14. if(players.size()>= 2){
    15. isStarting = true;
    16. if(!isStarting){
    17. i = 60;
    18. scheduler = getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    19. public void run(){
    20. if(i==45){
    21. getServer().broadcastMessage(ChatColor.GOLD + "[SnowBall] " + ChatColor.GREEN + String.valueOf(i) + ChatColor.AQUA + " seconds till the game starts!");
    22. }else if(i== 30){
    23. getServer().broadcastMessage(ChatColor.GOLD + "[SnowBall] " + ChatColor.GREEN + String.valueOf(i) + ChatColor.AQUA + " seconds till the game starts!");
    24. }else if(i== 15){
    25. getServer().broadcastMessage(ChatColor.GOLD + "[SnowBall] " + ChatColor.GREEN + String.valueOf(i) + ChatColor.AQUA + " seconds till the game starts!");
    26. }else if(i< 10 && i!= 0){
    27. getServer().broadcastMessage(ChatColor.GOLD + "[SnowBall] " + ChatColor.GREEN + String.valueOf(i) + ChatColor.AQUA + " seconds till the game starts!");
    28. }else if(i== 0){
    29. startGames();
    30. getServer().getScheduler().cancelTask(scheduler);
    31. }
    32. i = i-1;
    33. }
    34. }, 20L, 20L);
    35. }
    36. }
    37. }else{
    38. p.sendMessage(ChatColor.GOLD + "[SnowBall] " + ChatColor.RED + "The game is in progress, it will be done in: " + ChatColor.BLUE + left/60 + " mins!");
    39. }
    40. }
    41. }
    42. }
    43. }

     
  2. Offline

    Timr

    Check that the action is interacting with a block before you try and get the block, or you'll get errors like the ones you're experiencing.
     
Thread Status:
Not open for further replies.

Share This Page