[Solved] Teleport help - Thanks Darq!

Discussion in 'Plugin Development' started by Shadus, Aug 19, 2012.

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

    Shadus

    so I've got a semi functional teleport plugin written, the relevent portion of the code is below:
    Code:
    if(!this.getServer().getPlayer(args[i]).isOnline()) {
        ((Player) sender).sendMessage(tag + "Player not found.");
        return true;
    }
    ((Player) sender).teleport(this.getServer().getPlayer(args[i]));
    ((Player) sender).sendMessage("Teleporting you to " + args[i]);
    
    args-i at this point in the code has already been validated and will be the name of the person to teleport to... eg: /teleport phliptrip then args-i would be "phliptrip". now this works great provided the person is online and exists... however if i specify an offline user it explodes with:
    Code:
    03:50:02 (SEVERE) null
    org.bukkit.command.CommandException: Unhandled exception executing command 'teleport' in plugin pCommands v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
            at net.solatium.pcommands.PCommands.onCommand(PCommands.java:52)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 15 more
    
    and I'm not sure why, so far as i can tell in theory it should get caught at the if and return out... at least thats what i thought should work... but it doesn't seem to. Any idea why?

    (note: i am not a java programmer... yet. i have experience in perl, some c, bash, vb, etc... but I'm very rusty (15+ years) and this is my first go at java period.)
     
  2. Offline

    Darq

    You just need to do a null check on the player you're trying to get. This is why a NullPointerException was thrown
    Code:
    Player p = this.getServer().getPlayer(args[i]);
    if (p != null) {
        ((Player) sender).teleport(p.getLocation());
    }
    
     
    Shadus likes this.
  3. Offline

    Shadus

    Right on Darq! Thanks a ton!
     
Thread Status:
Not open for further replies.

Share This Page