Nullpointerexception with teleportation

Discussion in 'Plugin Development' started by Spadax, Sep 14, 2017.

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

    Spadax

    Hi, i created an homes systemb but, when i'm teleporting the player to his home, i'm getting an error in my console:

    Code:
    package fr.Spadax.Commands;
    
    import org.bukkit.Particle;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import fr.Spadax.YTool.YTool;
    
    public class Home implements CommandExecutor
    {
    
        YTool pl;
      
        public Home(YTool pl) {this.pl = pl;}
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, final String[] args)
        {  
            if(sender instanceof Player)
            {
                Player p = (Player) sender;
              
                if(args.length == 0)
                {
                    p.sendMessage("§8> §CBonne commande: /home <nom> ! (Fais /homes pour voir la liste de tes homes !)");
                    return true;
                }
              
                if(pl.homes.containsKey(p.getName().toLowerCase()+";"+args[0].toLowerCase()) && args.length == 1)
                {
                    p.playSound(p.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 1, 1);
                    p.getWorld().spawnParticle(Particle.DRAGON_BREATH, p.getLocation(), 30, 1, 1, 1, 0.01, null);
                    p.sendMessage("§8> §ATu as téléporté au home : §F"+args[0].toLowerCase()+" §A!");
                    p.teleport(pl.homes.get(p.getName().toLowerCase()+";"+args[0].toLowerCase()));
                }else p.sendMessage("§8> §CCet home n'existe pas ! (/homes ou /sethome)");
              
              
            }else sender.sendMessage("> Tu dois executer cette commande en tant que joueur !");
          
            return false;
        }
    
    }
    
    My error is displaying to give me the line 37 (p.teleport(...)) and to teleport the player, i'm getting the location in a hashmap. I don't understand why i'm getting that error beacause i'm checking if my hashmap (pl.homes) contains my location. Thanks for your help and sorry for my english !
     
  2. Offline

    ipodtouch0218

    Post the error using pastebin.com or hastebin.com
     
  3. Offline

    Spadax

    Code:
    [21:52:50] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'home' in plugin YTool v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Torch-f5a3e55]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:151) ~[spigot.jar:git-Torch-f5a3e55]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:673) ~[spigot.jar:git-Torch-f5a3e55]
        at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1386) ~[spigot.jar:git-Torch-f5a3e55]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1207) ~[spigot.jar:git-Torch-f5a3e55]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot.jar:git-Torch-f5a3e55]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[spigot.jar:git-Torch-f5a3e55]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[spigot.jar:git-Torch-f5a3e55]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_144]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_144]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Torch-f5a3e55]
        at org.torch.server.TorchServer.updateLogicsAndPhysics(TorchServer.java:897) [spigot.jar:git-Torch-f5a3e55]
        at org.torch.server.TorchServer.updateLogicsPhysicsExecuteCommands(TorchServer.java:1626) [spigot.jar:git-Torch-f5a3e55]
        at org.torch.server.TorchServer.tick(TorchServer.java:841) [spigot.jar:git-Torch-f5a3e55]
        at org.torch.server.TorchServer.run(TorchServer.java:550) [spigot.jar:git-Torch-f5a3e55]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer.teleport(CraftPlayer.java:588) ~[spigot.jar:git-Torch-f5a3e55]
        at org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity.teleport(CraftEntity.java:300) ~[spigot.jar:git-Torch-f5a3e55]
        at fr.Spadax.Commands.Home.onCommand(Home.java:43) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Torch-f5a3e55]
    @ipodtouch0218
     
  4. Offline

    EscapeMC

    Erm, two things that I think are right...

    In the p.teleport the location must be provided, not a name. Not sure how you have the running, but p.teleport requires a location.

    You should put more than just that class, as it would make it easier on us :p
     
  5. Online

    timtower Administrator Administrator Moderator

    @EscapeMC pl.homes is probably a hashmap<string,location>

    @Spadax Check if the key exists before you use it.
     
  6. Offline

    Spadax

    @EscapeMC My hashmap is <String,Location>

    @timtower But i'm checking no ? whith my pl.homes.containsKey() !
     
  7. Online

    timtower Administrator Administrator Moderator

    @Spadax Missed that one, could you post your onEnable?
     
  8. Offline

    EscapeMC

    Mmm, that makes sense.

    Do note though, @Spadax , that it is pointing to line 43, which is return false;
    Look into that :p
     
  9. Offline

    Spadax

    @EscapeMC the line 43 is like the 38 beacause i added a messages after.

    @timtower
    Code:
    public void setUpHomes()
        {
            FileConfiguration cgs;
            File folder = new File(getDataFolder()+"/Players");
            for(File f : folder.listFiles())
            {
                int i = 0;
                cgs = YamlConfiguration.loadConfiguration(f);
                if(cgs.contains(f.getName().replace(".yml", "")+".Homes"))
                {
                    ConfigurationSection cs = cgs.getConfigurationSection(f.getName().replace(".yml", "")+".Homes");
                    for(String home : cs.getKeys(false))
                    {
                        i++;
                        String[] part = cs.getString(home).split(";");
                        homes.put(f.getName().replace(".yml", "").toLowerCase()+";"+home.toLowerCase(), new Location(Bukkit.getWorld(part[0]), Double.valueOf(part[1]), Double.valueOf(part[2]), Double.valueOf(part[3]), Integer.parseInt(part[4]), Integer.parseInt(part[5])));
                    }
                }
                homenb.put(f.getName().replace(".yml", ""), i);
            }
           
            if(getConfig().getStringList("Limite Homes") == null)
            {
                getConfig().set("Limite Homes", Arrays.asList(new String[] {"ytool.home;1"}));
                saveConfig();
            }
            for(String perm : getConfig().getStringList("Limite Homes"))
            {
                homelimit.add(perm);
            }
        }
     
  10. Online

    timtower Administrator Administrator Moderator

    @Spadax That is not your onEnable.
    Please post your full main class.
     
Thread Status:
Not open for further replies.

Share This Page