Solved NPE Errors

Discussion in 'Plugin Development' started by nateracecar5, Jun 26, 2014.

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

    nateracecar5

    This code is giving me two issues actually:
    Code:java
    1. }else if(event.getPlayer().getItemInHand().getType() == Material.WOOD_SWORD){
    2. if(player.hasPermission("magictools.woodsword")){
    3. if(event.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Sword of Repelling")){
    4. //Block block = (Block) player.getTargetBlock(null, 50);
    5. Block block = (Block) player.getEyeLocation();
    6. block.setType(Material.COBBLESTONE);
    7. }else{
    8. return;
    9. }
    10. }else{
    11. player.sendMessage(ChatColor.AQUA + "[MagicTools] " + ChatColor.BLUE + "You don't have permission to use the wood sword ability. The admin may have disabled it to prevent world destruction.");
    12. }
    13. }else{
    14. return;
    15. }


    First off, it's giving me an NPE error on the line that checks the display name of the item. Secondly, how can I set a block 1 block above the target block location of the player? I can figure out the last one on my own if I tried, but I can't find out why the first one is giving me errors. Also, the sword is just named "Wooden Sword" because it is the default item name. Not changed at all. I need to make sure that the cobble isn't summoned if the name doesn't equal what is set.
     
  2. Offline

    Stealth2800

    If there's no display name then getDisplayName() is returning null, therefore causing an NPE. You can get the block on top of another block by doing block.getRelative(BlockFace.UP)
     
  3. Offline

    nateracecar5

    The block.getRelative worked, but I'm still getting an NPE, even after checking to make sure the Display Name didn't equal null. Updated code:
    Code:java
    1. }else if(event.getPlayer().getItemInHand().getType() == Material.WOOD_SWORD){
    2. if(player.hasPermission("magictools.woodsword")){
    3. if(event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Sword of Repelling") && !(event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Wood Sword"))){
    4. Block block = (Block) player.getTargetBlock(null, 50).getRelative(BlockFace.UP);
    5. block.setType(Material.WATER);
    6. }
    7. }else{
    8. player.sendMessage(ChatColor.AQUA + "[MagicTools] " + ChatColor.BLUE + "You don't have permission to use the wood sword ability. The admin may have disabled it to prevent world destruction.");
    9. }
    10. }else{
    11. return;
    12. }


    It's still giving me the same NPE Error on the line that checks the display name. I checked if it was null before, but that didn't work. Any help? Stealth2800
     
  4. Offline

    Stealth2800

    nateracecar5 The item will only return a display name if there is one set, otherwise it returns null. On that line, you're checking if the display name is equal to "Wood Sword", which I assume is your way to check if it has the default name. Use hasDisplayName() instead to see if the item has a display name before doing anything.
     
  5. Offline

    nateracecar5

    Thanks. Will try that.
     
  6. Offline

    AoH_Ruthless

    nateracecar5
    Make sure you check if it is null before checking what it equals to.
     
  7. Offline

    nateracecar5

    Solved. Thanks! Also, the plugin is finished thanks to you so I will post a link to it later. :) AoH_Ruthless Stealth2800
     
Thread Status:
Not open for further replies.

Share This Page