Solved Lets play a game! Who can find the error???

Discussion in 'Plugin Development' started by legostarwarszach, May 10, 2013.

Thread Status:
Not open for further replies.
  1. So heres my plugin that I'm failing at. it will teleport people just find and dandy. What it will not do is if the player is not online it will throw a NullPointerException. Can you find the stupid mistake?
    Code:
     if(commandLabel.equalsIgnoreCase("telep")){
                                if(arges.length==0){
                                    if((sender instanceof Player)){
                                        Player player = (Player) sender;
                                    player.sendMessage(ChatColor.AQUA + "Teleporter V.Beta 1.0");
                                        player.sendMessage(ChatColor.AQUA + "Plugin made by: Zach Farley");
                                    }else if(!(sender instanceof Player)){
                                            this.logger.severe("You must be a player to use this command!");
                                        }
                                }else if(arges.length == 1){
                                    if(!(sender instanceof Player)){
                                        this.logger.severe(ChatColor.DARK_RED + "You must be a player to use this command!");
                                    }else if((sender instanceof Player)){
                                        Player player = (Player) sender;
                                        Player tp = player.getServer().getPlayer(arges[0]);
                                            if((tp.isOnline())){                             
                                                Location tploc = tp.getLocation();
                                                player.teleport(tploc);
                                            }if(!(tp.isOnline())){
                                                player.sendMessage(ChatColor.DARK_RED + "Could not find player " + arges[0]);
                                            }
     
  2. Offline

    RainoBoy97

    Code:
    if(tp != null){
    
     
  3. Offline

    Suraski

    Use a try catch block?
     
  4. Offline

    Ivan

    Why don't you just read the stacktrace so you can see what line the error is on...
    Isn't it obvious that the first step in solving errors is taking a look at the stacktrace. :confused:
     
    ferrybig likes this.
  5. Offline

    Nova20012

    You wrote:
    Code:
    player.sendMessage(ChatColor.DARK_RED + "Could not find player " + arges[0]);
    I can spot one problem here that can throw an error, thats 'arges[0];' that should read: 'args[0];'.

    So that whole line:
    Code:
    player.sendMessage(ChatColor.DARK_RED + "Could not find player " + args[0]);
    Another is the referencing the player as tp, it should read:
    Code:
    Player tp = player.getServer().getPlayer(args[0]);
    So the whole corrected code should read:
    Code:
    if(commandLabel.equalsIgnoreCase("telep")){
        if(args.length == 0){
            if(sender instanceof Player){
                Player player = (Player) sender;
                player.sendMessage(ChatColor.AQUA + "Teleporter V.Beta 1.0");
                player.sendMessage(ChatColor.AQUA + "Plugin made by: Zach Farley");
            }else{
              System.out.println("You must be a player to do this!");
            }
        }else if(args.length == 1){
            if(sender instanceof Player){
                Player player = (Player) sender;
                Player tp = player.getServer().getPlayer(args[0]);
                    if((tp.isOnline())){                         
                        Location tploc = tp.getLocation();
                        player.teleport(tploc);
                    }if(!(tp.isOnline())){
                        player.sendMessage(ChatColor.DARK_RED + "Could not find player " + args[0]);
                    }
            }else{
                System.out.println("Must be a player!");
            }
        }
    }
        
    EDIT: Though this is makes no difference if your 'onCommand()' method is set out with 'String[] arges' as the final argument of the method.
     
  6. Offline

    macguy8

    Nova20012
    If you notice, it is all throughout his code, meaning he named the variable to that, instead of args. That is not an error, just a preference of spelling.

    legostarwarszach
    Code:
    if (tp == null) {
      player.sendMessage("Player not online");
    }
     
  7. Offline

    Nova20012

    macguy8
    He could have spelt it like 'args' in his onCommand method though.
     
  8. Offline

    MP5K

    why don't you just change:
    PHP:
    if((tp.isOnline())){      
    to
    PHP:
    if( tp != null ) {      
     
  9. RainoBoy97 is the winner! This was the first and the easiest method. Thanks guys for playing!
     
  10. Offline

    macguy8

    Nova20012
    But that's not an error. I can spell my args variable "somerandomvariablethatdoessomethingmagical" and it wouldn't be an error as long as I always type it the same way.
     
Thread Status:
Not open for further replies.

Share This Page