Solved NullPointer... Help

Discussion in 'Plugin Development' started by Tomasko, Jan 17, 2014.

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

    Tomasko

    Hello, I get null pointer exeption, but i don't know, where is problem
    [​IMG]
    Code:java
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6.  
    7. package magicbattle;
    8.  
    9. import java.util.Arrays;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.entity.Snowball;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.potion.PotionEffect;
    22. import org.bukkit.potion.PotionEffectType;
    23.  
    24. /**
    25. *
    26. * @author Tomas
    27. */
    28. public class ResistanceField implements Listener {
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35. public void giveResisField(Player p) {
    36. ItemStack wand = new ItemStack(Material.PAPER, 1);
    37. ItemMeta meta = wand.getItemMeta();
    38. meta.setDisplayName(ChatColor.GREEN + "Field of 10 seconds resistance");
    39. meta.setLore(Arrays.asList("RightClick to get resistance", "LeftClick give resistance", "Use it wisely."));
    40. wand.setItemMeta(meta);
    41.  
    42. p.getInventory().addItem(wand);
    43. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "[MagicBattle]" + ChatColor.RESET + "" + ChatColor.BLUE + " You now get 10 seconds resistance field");
    44. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "[MagicBattle]" + ChatColor.RESET + "" + ChatColor.GRAY + " Right-Click to get resistance");
    45. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "[MagicBattle]" + ChatColor.RESET + "" + ChatColor.GRAY + " Left-click to give resistance");
    46. }
    47.  
    48. @EventHandler
    49. public void onFireWandUse(PlayerInteractEvent e) {
    50. Player p = e.getPlayer();
    51. if (p.getInventory().getItemInHand() == null) return;
    52. if (!(p.getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Field of 10 seconds resistance"))) return;
    53. if (e.getAction() == Action.RIGHT_CLICK_AIR ) {
    54. p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 200, 100));
    55. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "[MagicBattle]" + ChatColor.RESET + "" + ChatColor.GREEN + " You have got now resistance for damage for 10 seconds");
    56. p.getInventory().remove(Material.PAPER);
    57.  
    58. }
    59. }
    60. @EventHandler
    61. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    62. if (event.getDamager() instanceof Player ) {
    63. Player attacker = (Player) event.getDamager();
    64. Player damaged = (Player) event.getEntity();
    65. if (attacker.getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Field of 10 seconds resistance")) {
    66. if (attacker.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Field of 10 seconds resistance")) {
    67. PotionEffect potionEffect = new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 200, 100);
    68. potionEffect.apply(damaged);
    69. attacker.getInventory().remove(Material.PAPER);
    70. damaged.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "[MagicBattle]" + ChatColor.RESET + "" + ChatColor.GREEN + " You have got now resistance for damage for 10 seconds");
    71. }
    72. }
    73. } }
    74.  
    75.  
    76. }


    Thanks for your time and answers :)
     
  2. Offline

    NoLiver92

    Read this link, this is how to read your own stack traces so that you know where the error is.

    And for your error on line 52, you need to check if the item has metadata, if it doesnt this will be causing the null error
     
  3. Offline

    Tomasko

    I know stack traces, I did not notice that metadata can be null :/ (Yes, I'm too noobish)
    Thanks! :)

    //Edit Sorry, but I have one more question :/
    Code:java
    1. public class Arena {
    2.  
    3. Logger log = Logger.getLogger("Minecraft");
    4.  
    5. public double x1 = 5.0;
    6. public double y1 = 5.0;
    7. public double z1 = 50.0;
    8.  
    9. public void setUp(double x, double y, double z) {
    10. this.x1 = x;
    11. this.y1 = y;
    12. this.z1 = z;
    13. log.info(x1 + " " + y1 + " " + z1);
    14. }
    15.  
    16. public void joinArena(Player p, int classid) {
    17. Location l = p.getLocation();
    18. log.info(x1 + " " + y1 + " " + z1);
    19. l.setX(x1);
    20. l.setY(y1);
    21. l.setZ(z1);
    22. p.sendMessage(x1 + " " + y1 + " " + z1);
    23. p.teleport(l);
    24. }
    25.  
    26. }

    setUp has setted values(OK, and log says good values), but joinArena gets setted values(public double x1 = 5.0 public double y1 = 5.0 public double z1 = 50.0)
    I thing, JoinArena should get values that was setted in setUp but it doesn't
    values are same with this
    setUp be called in onEnable()

    P.S. sorry, but i dont know :(
     
  4. Offline

    NoLiver92


    umm im not sure what your asking for here, but if the location is not the location from setup it may be an issue on how you are creating the location
     
  5. Offline

    Tomasko

    In setUp i set values, but joinArena behaves as if the value were not at all set in the setUp. Setting values ​​were 40.0, 30.6, 23.5 being JoinArena right as if you are setting was not at all, I need to set double x, y, z class Arena while the value will be taken for all methods (this should be).
    So the problem is: in the setup were set certain values​​, while JoinArena method behaves "as if" they did not see, because it uses default values ​​(defined as double x = 0.0) where JoinArena dont reciving value commissioned by setUp but JoinArena uses the default value, so for example 0.0
     
  6. Offline

    NoLiver92

    why not do something like this:
    Code:java
    1. World world = player.getWorld();
    2. Location l = new Location(world, this.x1, this.y1, this,z1);
    3. p.teleport(l);


    I believe that this should work (not tested)
     
  7. Offline

    Tomasko

    I say, problem isn't in teleportation but in getted values, values are setted in setUp but JoinArena don't get values setted in setUp
    code:
    Code:java
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6.  
    7. package magicbattle;
    8.  
    9.  
    10. import java.util.logging.Logger;
    11. import org.bukkit.Location;
    12. import org.bukkit.World;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15.  
    16.  
    17. /**
    18. *
    19. * @author Tomas
    20. */
    21. public class Arena {
    22.  
    23. Logger log = Logger.getLogger("Minecraft");
    24.  
    25. public double x1 = 0.0;
    26. public double y1 = 0.0;
    27. public double z1 = 0.0;
    28.  
    29. public void setUp(double x, double y, double z) {
    30. this.x1 = x;
    31. this.y1 = y;
    32. this.z1 = z;
    33. log.info(x1 + " " + y1 + " " + z1);
    34. }
    35.  
    36. public void joinArena(Player p, int classid) {
    37. World world = p.getWorld();
    38. Location l = new Location(world, this.x1, this.y1, this.z1);
    39. p.sendMessage(x1 + " " + y1 + " " + z1);
    40. p.teleport(l);
    41. }
    42.  
    43. }
    44.  


    I log values setted in setup - they are OK but values in JoinArena are still default, for explame in top of program is public double x1 = 0.0
    setUp set x1 to blabla in onEnable
    JoinArena gets x1 as 0.0, i thing joinarena dont get setted values, but default from public double x1 = 0.0
     
  8. Offline

    NoLiver92

    ok i think i know whats wrong, you need to save the instance of this file somewhere with the values set up, then call the instance when you want to use the next value otherwise a new instance wont contain the set up values.
     
  9. Offline

    xTigerRebornx

    Tomasko You are probably not using the same Arena object as the one you've set your values for.
     
  10. Offline

    Tomasko

Thread Status:
Not open for further replies.

Share This Page