Solved Scoreboard Null Pointer

Discussion in 'Plugin Development' started by Buizelfan2, Aug 27, 2014.

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

    Buizelfan2

    I'm testing things with scoreboard and when I added a point every time a player broke a block It works fine but when I tried adding a point when a projectile hits something It doesn't work It give me a NPE and I'm not sure how to fix it

    Code:
    Code:java
    1. Scoreboard board;
    2. Score score;
    3.  
    4. public void onEnable() {
    5. makeScoreboard();
    6. }
    7.  
    8. public void makeScoreboard() {
    9. ScoreboardManager manager = Bukkit.getScoreboardManager();
    10. board = manager.getNewScoreboard();
    11.  
    12. Objective objective = board.registerNewObjective("Test", "Test2");
    13. objective.setDisplayName(ChatColor.AQUA + "Points");
    14. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    15.  
    16. score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Points"));
    17. }
    18.  
    19. public void onJoin(PlayerJoinEvent e) {
    20. e.getPlayer().setScoreboard(board);
    21. }
    22.  
    23. @SuppressWarnings("deprecation")
    24. @EventHandler
    25. public void onShoot(PlayerInteractEvent e) {
    26. Player p = e.getPlayer();
    27. if (e.getAction() != Action.RIGHT_CLICK_AIR) {
    28. return;
    29. } else if (p.getItemInHand().getType() == Material.IRON_AXE) {
    30. p.throwSnowball();
    31. }
    32. }
    33.  
    34. @SuppressWarnings("deprecation")
    35. @EventHandler
    36. public void onHit(ProjectileHitEvent event) {
    37. BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
    38. Block hitBlock = null;
    39. while (iterator.hasNext()) {
    40. hitBlock = iterator.next();
    41. if (hitBlock.getType() != null) {
    42. Location breakBlocks = hitBlock.getLocation();
    43. breakBlocks.getWorld().playEffect(hitBlock.getLocation(), Effect.STEP_SOUND, hitBlock.getTypeId());
    44. breakBlocks.getBlock().setType(Material.AIR);
    45. score.setScore(score.getScore() + 1);
    46. }
    47. }
    48. }


    Line I'm getting the NPE:
    Code:java
    1. score.setScore(score.getScore() + 1);
     
  2. Offline

    fireblast709

    Buizelfan2 which means score is null. onEnable isn't being called, and I assume the cause for that is because this isn't your main class. In order to get it working, call onEnable after registering the Listener.
     
  3. Offline

    Buizelfan2

    Okay now the scoreboard works but now it's not adding one it's adding anywhere from 4-6
    Edit:
    I tested some things out to see what the problem is.
    When I hit a block that is not wool I get this
    Not Wool
    But when I hit a block that is wool I get this
    Hit Wool
    So It's like I'm hitting more then one block but I'm not sure what could be causing that
    Code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onHit(ProjectileHitEvent event) {
    4. Player p = (Player) event.getEntity().getShooter();
    5. BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
    6. Block hitBlock = null;
    7. while (iterator.hasNext()) {
    8. hitBlock = iterator.next();
    9. if (hitBlock.getType() != null) {
    10. if (hitBlock.getType() == Material.WOOL) {
    11. p.sendMessage("Hit");
    12. } else {
    13. p.sendMessage("Not Wool");
    14. }
    15. }
    16. }
    17. }
     
  4. Offline

    fireblast709

    Buizelfan2 you loop through all blocks that could be your target within 4 distance. If you just want to hit the first wool block you encounter, add a break; statement after p.sendMessage("hit")
     
  5. Offline

    Buizelfan2

    Now it comes up with
    "Not Wool"
    "Hit"
     
  6. Offline

    fireblast709

    Buizelfan2 which is what you want, no? If you want it to ignore AIR, you should check for getType() != Material.AIR instead of getType() != null ;3
     
  7. Offline

    Buizelfan2

    What ever kind of block I hit I get this
    Block Hit
    Code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onHit(ProjectileHitEvent event) {
    4. Player p = (Player) event.getEntity().getShooter();
    5. BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);
    6. Block hitBlock = null;
    7. while (iterator.hasNext()) {
    8. hitBlock = iterator.next();
    9. if (hitBlock.getType() == Material.AIR) {
    10. if (hitBlock.getType() == Material.WOOL) {
    11. p.sendMessage("Hit");
    12. break;
    13. } else {
    14. p.sendMessage("Not Wool");
    15. break;
    16. }
    17. }
    18. }
    19. }
     
  8. Offline

    fireblast709

    Buizelfan2 Just noticed a typo in my post. It is supposed to be != Material.AIR
     
  9. Offline

    Buizelfan2

    Okay it works now thanks for the help
     
Thread Status:
Not open for further replies.

Share This Page