Solved [NPE] Getting the player from arguments.

Discussion in 'Plugin Development' started by Maved, Jul 30, 2014.

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

    Maved

    I'm going to start off with the code:

    Code:java
    1. Player target = getServer().getPlayer(args[1]);
    2.  
    3. if (args[0].equalsIgnoreCase("feed"))
    4. {
    5. if(target != null)
    6. {
    7. target.setFoodLevel(20);
    8. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c has been fed!");
    9. }else{
    10. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c is not online!");
    11. }
    12.  
    13.  
    14. }
    15.  
    16. if (args[0].equalsIgnoreCase("heal"))
    17. {
    18. if(target != null)
    19. {
    20. target.setHealth(20.0);
    21. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c has been healed!");
    22. }else{
    23. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c is not online!");
    24. }
    25.  
    26. }

    So basically my first issue is, if it is null it will just give me a NPE even though i have checked if it was null, the second issue is, my IGN is Maved145 and if i do the command it heals me, but if i do the command and just put "m" in the name it will heal me still o.o

    If you could help it will be greatly appreciated!
     
  2. Offline

    jimbo8

    Check for the player after you have checked for arguments. I could tell by just reading the title ;)

    edit:

    Didn't read your second "issue". It's supposed to work like that, if you write d14 then it would still heal you. Bukkit checks for the name closest to your arguments.
     
  3. Offline

    Maved

    jimbo8
    I'll try check for the player after the arguments and i didn't know that! Thats a cool feature!
     
    jimbo8 likes this.
  4. Offline

    Gnat008

    Maved
    Also be sure to check if the arguments exist in the first place:
    Code:
    // EXAMPLE:
    if (args.length == 1) {
        // code
    } else if (args.length >= 2) {
        // code
    }
     
  5. Offline

    Maved

    Gnat008
    I already did that just didn't show it in the code :p

    Code:java
    1. if (args[0].equalsIgnoreCase("feed"))
    2. {
    3. Player target = getServer().getPlayer(args[1]);
    4. if(target != null)
    5. {
    6. target.setFoodLevel(20);
    7. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c has been fed!");
    8. }else{
    9. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c is not online!");
    10. }
    11.  
    12.  
    13. }
    14.  
    15. if (args[0].equalsIgnoreCase("heal"))
    16. {
    17. Player target = getServer().getPlayer(args[1]);
    18. if(target != null)
    19. {
    20. target.setHealth(20.0);
    21. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c has been healed!");
    22. }else{
    23. player.sendMessage(ChatUtil.prefix() + ChatColor.GRAY + target.getName() + "§c is not online!");
    24. }
    25.  
    26. }


    I now got that and its still not working :L any help?
     
  6. Offline

    redside100

    Maved Try to do Bukkit.getServer().getPlayer(String)
     
  7. Offline

    fireblast709

    Maved you check if it's null... And still use it in the else statement.
     
  8. Offline

    Maved

    fireblast709 As you can see i put the topic as solved a while before you commented, i fixed it now :)
     
Thread Status:
Not open for further replies.

Share This Page