Solved cannot be resolved

Discussion in 'Plugin Development' started by kamakarzy, May 5, 2013.

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

    kamakarzy

    can anyone help im having problem with my almost finished code i kept getting nulpointer so i tried
    fixing it i know im close but i cant just quite put my finger on what im missing


    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args ){
    3. final Player player1 = (Player) sender;
    4. if (commandLabel.equalsIgnoreCase("hugs")){
    5. if(args.length == 0){
    6. player1.sendMessage(ChatColor.RED + "You need to Choose a Player");
    7. }
    8. else if(args.length == 1){
    9. if (Bukkit.getServer().getPlayer(args[0]) != null){
    10. String Player2 = Bukkit.getServer().getPlayer(args[0]).getName();
    11. }else{
    12. player1.sendMessage("Player is Offline");}
    13. }else{
    14. player1.sendMessage(ChatColor.RED + "Unknown player");
    15. return true;
    16. }
    17. }
    18. if(player1.getLocation().distanceSquared(getServer().getPlayer(Player2).getLocation()) <= 4) {
    19. BukkitRunnable run = new BukkitRunnable(){
    20.  
    21. private int count = 0;
    22.  
    23. @Override
    24. public void run() {
    25. if(count >= 4){
    26. this.cancel();
    27. return;
    28. }
    29. hearts(player1);
    30. count++;
    31.  
    32. }
    33.  
    34. };
    35. run.runTaskTimer(this, 0L, 20L);
    36. getServer().getPlayer(Player2).sendMessage(player1 + "just gave you a hug");
    37. }
    38. else{
    39. player1.sendMessage(ChatColor.RED + "player too far away");
    40. }
    41. return true;
    42. }
    43. }
     
  2. Offline

    MP5K

    Hello kamakarzy,
    where is the Exception occurring?
     
  3. Offline

    kamakarzy

    MP5K on player 2

    if(player1.getLocation().distanceSquared(getServer().getPlayer(Player2).getLocation()) <= 4) {
    BukkitRunnable run = new BukkitRunnable(){

    getServer().getPlayer(Player2).sendMessage(player1 + "just gave you a hug");

    ERROR: Player2 cannot be resolved into a veriable

    @MP5K
    any ideas ? :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    MP5K

    change: String Player2 = Bukkit.getServer().getPlayer(args[0]).getName();
    to: final String Player2 = Bukkit.getServer().getPlayer(args[0]).getName();
     
  5. Offline

    kamakarzy

    MP5K nope stays same

    attached a pic for u
     

    Attached Files:

  6. Offline

    kamakarzy

  7. kamakarzy
    Learn to use indentation, your IDE can do it for you easily.
    That way you can clearly see the code and the scope of variables... in that code your Player2 variable (which is badly named, don't start fields/variables/package names with upper case letters) is within the condition scope...

    Indented code:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
        final Player player1 = (Player)sender; // this will throw an error when command is used from console
        
        if(commandLabel.equalsIgnoreCase("hugs")) // use cmd.getName() instead
        {
            if(args.length == 0)
            {
                // you don't need a Player object to send messages, sender.sendMessage() works too...
                player1.sendMessage(ChatColor.RED + "You need to Choose a Player");
            }
            else if(args.length == 1)
            {
                if(Bukkit.getServer().getPlayer(args[0]) != null) // why are you calling getPlayer() more than once ? Store it properly
                {
                    String Player2 = Bukkit.getServer().getPlayer(args[0]).getName();
                    // Your issue:
                    // You create 'Player2' here, therefore it's only accessible in this code block
                    // Create it next to 'player1' and don't asign it.
                }
                else
                {
                    player1.sendMessage("Player is Offline");
                    // you should return here as you did below.
                }
            }
            else
            {
                player1.sendMessage(ChatColor.RED + "Unknown player");
                return true;
            }
        }
        
        if(player1.getLocation().distanceSquared(getServer().getPlayer(Player2).getLocation()) <= 4)
        {
            BukkitRunnable run = new BukkitRunnable()
            {
                
                private int count = 0;
                
                @Override
                public void run()
                {
                    if(count >= 4)
                    {
                        this.cancel();
                        return;
                    }
                    hearts(player1);
                    count++;
                    
                }
                
            };
            run.runTaskTimer(this, 0L, 20L);
            getServer().getPlayer(Player2).sendMessage(player1 + "just gave you a hug"); // why are you using getPlayer() again ?! Just store the player pointer.
        }
        else
        {
            player1.sendMessage(ChatColor.RED + "player too far away");
        }
        return true;
    }
    See code comments.

    EDIT:
    I see you have somewhat of indentation there, it's still a bit off so I suggest using your IDE's format option.... Ctrl+Shift+F for Eclipse.
     
  8. Offline

    kamakarzy

    Digi thanks i think i got what you ment and now i just get a
    Code:
    2013-05-05 15:31:12 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hugs' in plugin Huggs v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_5_R2.PlayerConnection.handleCommand(PlayerConnection.java:966)
        at net.minecraft.server.v1_5_R2.PlayerConnection.chat(PlayerConnection.java:884)
        at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java:841)
        at net.minecraft.server.v1_5_R2.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_5_R2.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java:110)
        at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:580)
        at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:225)
        at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
        at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
        at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.getPlayer(CraftServer.java:323)
        at com.gmail.nomad856.kamakarzy.Main.onCommand(Main.java:62)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    Code:java
    1. package com.gmail.nomad856.kamakarzy;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.EntityEffect;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.entity.Wolf;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.scheduler.BukkitRunnable;
    15.  
    16. public class Main extends JavaPlugin {
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18. public static Main plugin;
    19.  
    20. @Override
    21. public void onEnable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion()
    24. + "Has Been Enabled");
    25. }
    26.  
    27. @Override
    28. public void onDisable() {
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info(pdfFile.getName() + "Has Been Disabled");
    31. }
    32.  
    33. public void hearts(Player p) {
    34. Wolf o = p.getWorld().spawn(p.getLocation(), Wolf.class);
    35. o.playEffect(EntityEffect.WOLF_HEARTS);
    36. o.remove();
    37. }
    38.  
    39. public boolean onCommand(CommandSender sender, Command cmd,
    40. String commandLabel, String[] args) {
    41. final Player player1 = (Player) sender;
    42. final String player2 = null;
    43. if (commandLabel.equalsIgnoreCase("hugs")) {
    44. if (args.length == 0) {
    45. sender.sendMessage(ChatColor.RED
    46. + "You need to Choose a Player");
    47. } else if (args.length == 1) {
    48. if (Bukkit.getServer().getPlayer(args[0]) != null) {
    49.  
    50. } else {
    51. sender.sendMessage("Player is Offline");
    52. }
    53. return true;
    54. } else {
    55. sender.sendMessage(ChatColor.RED + "Unknown player");
    56. return true;
    57. }
    58. }
    59. if (player1.getLocation().distanceSquared(
    60. getServer().getPlayer(player2).getLocation()) <= 4) {
    61. BukkitRunnable run = new BukkitRunnable() {
    62.  
    63. private int count = 0;
    64.  
    65. @Override
    66. public void run() {
    67. if (count >= 4) {
    68. this.cancel();
    69. return;
    70. }
    71. hearts(player1);
    72. count++;
    73.  
    74. }
    75.  
    76. };
    77. run.runTaskTimer(this, 0L, 20L);
    78. getServer().getPlayer(player2).sendMessage(
    79. player1 + "just gave you a hug");
    80. } else {
    81. player1.sendMessage(ChatColor.RED + "player too far away");
    82. }
    83. return true;
    84. }
    85. }
    86.  
     
  9. kamakarzy
    That 2nd part of code must not be reached if player2 is null...
     
  10. Offline

    kamakarzy

    Digi anything you could suggest i had it working b4 all i wanted to do is put a check in for if player was off line to stop null point exception
     
  11. Offline

    StaticE

    kamakarzy Not sure if you did this on purpose, but you literally set Player2 to null.
    Code:
    final String player2 = null;
    Edit: I'm pretty sure you need to make Player2 an actual player instead of a string to use stuff like getLocation, etc. as well.
     
  12. Offline

    kamakarzy

    StaticE & Digi
    i decided there must be a betterway to check if the player is online so if you could look through this and tell me if there is
    Code:java
    1. package com.gmail.nomad856.kamakarzy;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.EntityEffect;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.entity.Wolf;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.scheduler.BukkitRunnable;
    14.  
    15. public class Main extends JavaPlugin {
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. public static Main plugin;
    18.  
    19. @Override
    20. public void onEnable() {
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion()
    23. + "Has Been Enabled");
    24. }
    25.  
    26. @Override
    27. public void onDisable() {
    28. PluginDescriptionFile pdfFile = this.getDescription();
    29. this.logger.info(pdfFile.getName() + "Has Been Disabled");
    30. }
    31.  
    32. public void hearts(Player p) {
    33. Wolf o = p.getWorld().spawn(p.getLocation(), Wolf.class);
    34. o.playEffect(EntityEffect.WOLF_HEARTS);
    35. o.remove();
    36. }
    37.  
    38. public boolean onCommand(CommandSender sender, Command cmd,
    39. String commandLabel, String[] args) {
    40. final Player player1 = (Player) sender;
    41. Player player2 = sender.getServer().getPlayer(args[0]);
    42. if (commandLabel.equalsIgnoreCase("hugs")) {
    43. if (args.length == 0) {
    44. if(player1.getLocation().distanceSquared(player2.getLocation()) <= 4) {
    45. }
    46. BukkitRunnable run = new BukkitRunnable() {
    47.  
    48. private int count = 0;
    49.  
    50. @Override
    51. public void run() {
    52. if (count >= 4) {
    53. this.cancel();
    54. return;
    55. }
    56. hearts(player1);
    57. count++;
    58.  
    59. }
    60.  
    61. };
    62. run.runTaskTimer(this, 0L, 20L);
    63. player2.sendMessage(player1 + "just gave you a hug");
    64. } else {
    65. sender.sendMessage(ChatColor.RED + "player too far away");
    66. }
    67. }
    68. return true;
    69. }
    70. }
     
  13. kamakarzy
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equals("hugs"))
            {
                if(sender instanceof Player)
                {
                    if(args.length > 0)
                    {
                        Player target = Bukkit.getPlayer(args[0]);
                        
                        if(target != null)
                        {
                            Player player = (Player)sender;
                            Location playerLoc = player.getLocation();
                            Location targetLoc = target.getLocation();
                            
                            if(playerLoc.getWorld().getUID().equals(targetLoc.getWorld().getUID()))
                            {
                                if(playerLoc.distanceSquared(targetLoc) < 4) // this is max 2 blocks away.
                                {
                                    target.setFireTicks(20 * 5); // hehehe :}
                                    // but seriously, do your thing here.
                                    
                                    target.sendMessage(player.getDisplayName() + " hugs you.");
                                }
                                else
                                {
                                    sender.sendMessage(target.getDisplayName() + " is too far away.");
                                }
                            }
                            else
                            {
                                sender.sendMessage(target.getDisplayName() + " is not in the same world as you.");
                            }
                        }
                        else
                        {
                            sender.sendMessage("Can't find player: " + args[0]);
                        }
                    }
                    else
                    {
                        return false; // print usage
                    }
                }
                else
                {
                    sender.sendMessage("This command can only be used by a player.");
                }
            }
            
            return true;
        }
    
     
  14. Offline

    kamakarzy

    Digi

    thanks and the set the player on fire was a good idea but ill leave that for a different plugin :)
     
Thread Status:
Not open for further replies.

Share This Page