Error in my plugin when i setlobby

Discussion in 'Spigot Plugin Development' started by --TOMAS--, May 19, 2016.

  1. Error when i execute the command /setlobby
    Code (Text):
    [CODE][21:53:46] [Server thread/INFO]: FiesteroCraft issued server command: /setlobby
    [21:53:46] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setlobby' in plugin UltraLobby v4.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1350) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1185) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.NullPointerException
        at me.FiesteroCraft.UltraLobby.commands.SetLobbyCmd.onCommand(SetLobbyCmd.java:41) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.2.jar:git-Spigot-5a40365-f631379]
        ... 15 more
    [/CODE]

    CLASSES:
    Code (Text):
    package me.FiesteroCraft.UltraLobby.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.FiesteroCraft.UltraLobby.Main;

    public class SetLobbyCmd implements CommandExecutor {
        private Main plugin;
       
        public SetLobbyCmd(Main pl){
            plugin = pl;
           
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.DARK_RED + "You cannot execute this command from console");
                return false;
            }
       
            Player player = (Player) sender;
            if ((cmd.getName().equalsIgnoreCase("setlobby"))) {
                if ((player.hasPermission("ultralobby.lobby.set")) || (player.isOp()))
                {
                  if (args.length == 0)
                  {
                      plugin.getConfig().set("Lobby.x", Double.valueOf(player.getLocation().getX()));
                      plugin.getConfig().set("Lobby.y", Double.valueOf(player.getLocation().getY()));
                      plugin.getConfig().set("Lobby.z", Double.valueOf(player.getLocation().getZ()));
                      plugin.getConfig().set("Lobby.pitch", Float.valueOf(player.getLocation().getPitch()));
                      plugin.getConfig().set("Lobby.yaw", Float.valueOf(player.getLocation().getYaw()));
                      plugin.getConfig().set("world", player.getWorld().getName());
                      plugin.saveConfig();
                    sender.sendMessage(plugin.getConfig().getString("lobbySetted")
                            .replaceAll("&", "§"));
                  if (args.length == 1)
                  {
                    String lobby = args[0];
                    plugin.getConfig().addDefault("Lobby.Specifics", lobby);
                    plugin.getConfig().set("x-" + lobby, Double.valueOf(player.getLocation().getX()));
                    plugin.getConfig().set("y-" + lobby, Double.valueOf(player.getLocation().getY()));
                    plugin.getConfig().set("z-" + lobby, Double.valueOf(player.getLocation().getZ()));
                    plugin.getConfig().set("pitch-" + lobby, Float.valueOf(player.getLocation().getPitch()));
                    plugin.getConfig().set("yaw-" + lobby, Float.valueOf(player.getLocation().getYaw()));
                    plugin.getConfig().set("world-" + lobby, player.getWorld().getName());
                    plugin.saveConfig();
                    sender.sendMessage(plugin.getConfig().getString("lobbySetted")
                            .replaceAll("&", "§"));
                  }
                }
                else
                {
                  sender.sendMessage(Main.sinpermisos.replaceAll("&", "§"));
                }
              }
            }
            return true;

        }
    }
     
    Code (Text):
    package me.FiesteroCraft.UltraLobby.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.FiesteroCraft.UltraLobby.Main;

    public class LobbyCmd implements CommandExecutor{
       
        private Main plugin;
       
        public LobbyCmd(Main pl){
            plugin = pl;
           
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
            {
            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.DARK_RED + "You cannot execute this command from console");
                return false;
            }
                  Player player = (Player)sender;
                  if ((cmd.getName().equalsIgnoreCase("lobby")))
                  {
                    if (args.length == 0) {
                      if ((plugin.getConfig().getString("Lobby.x") == null) || (plugin.getConfig().getString("Lobby.y") == null) || (plugin.getConfig().getString("Lobby.z") == null) || (plugin.getConfig().getString("Lobby.yaw") == null) || (plugin.getConfig().getString("Lobby.pitch") == null)) {
                        sender.sendMessage(plugin.getConfig().getString("Messages.lobbyNotSetted")
                                .replaceAll("&", "§"));
                      } else {
                        player.teleport(new Location(
                          Bukkit.getServer().getWorld(plugin.getConfig().getString("Lobby")),
                          plugin.getConfig().getDouble("Lobby.x"),
                          plugin.getConfig().getDouble("Lobby.y"),
                          plugin.getConfig().getDouble("Lobby.z"),
                          plugin.getConfig().getInt("Lobby.yaw"),
                          plugin.getConfig().getInt("Lobby.pitch")));
                      }
                    }
                    if (args.length == 1)
                    {
                      String lobby = args[0];
                      if ((plugin.getConfig().getString("x-" + lobby) == null) || (plugin.getConfig().getString("y-" + lobby) == null) || (plugin.getConfig().getString("z-" + lobby) == null) || (plugin.getConfig().getString("yaw-" + lobby) == null) || (plugin.getConfig().getString("pitch-" + lobby) == null)) {
                            sender.sendMessage(plugin.getConfig().getString("Messages.lobbyNotSetted")
                                    .replaceAll("&", "§"));
                      } else {
                        player.teleport(new Location(
                          Bukkit.getServer().getWorld(plugin.getConfig().getString("world-" + lobby)),
                          plugin.getConfig().getDouble("x-" + lobby),
                          plugin.getConfig().getDouble("y-" + lobby),
                          plugin.getConfig().getDouble("z-" + lobby),
                          plugin.getConfig().getInt("yaw-" + lobby),
                          plugin.getConfig().getInt("pitch-" + lobby)));
                      }
                    }
                  }
            return true;
        }
    }
     
     
  2. This is called a null pointer exception. It means you are trying to use data which simply does not exist, and is very common.

    Learn basic java, it will help.
     
  3. Neither of your classes really help us. I recommend learning Java and how to read stack traces. In cases like this, you wouldn't need to ask questions, as it's just simple reading and solving. I'm going to leave you with some advise to fixing your code, and how to fix your error.

    • Name your packages with all lowercase, according to Java code conventions.
    • I don't believe you need to check if someone is op, as they have all permissions by default.
    • Don't use replaceAll("&", "§")). This will replace and and all &'s with them. What if you wanted something like "Line 1 & Line 2"? That would be replaced with "Line 1 § Line 2", and would mess it up. You also shouldn't use this symbol for compatibility issues. What if Mojang changes the symbol? Instead, stick with the ChatColor.translateAlternateColorCodes(char c, String s) method.
    And regarding your error:

    Caused by: java.lang.NullPointerException
    at me.FiesteroCraft.UltraLobby.commands.SetLobbyCmd.onCommand(SetLobbyCmd.java:41) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.2.jar:git-Spigot-5a40365-f631379]

    There is something null in your SetLobbyCmd class on line 41.