Solved Unhandled exception executing command

Discussion in 'Plugin Development' started by Veqtive, Dec 11, 2018.

Thread Status:
Not open for further replies.
  1. Hey, I'm trying to develop a sethome Plugin, but if I just type /home or /sethome, it just gives me this error message
    Code:
    issued server command: /sethome
    [17:37:05 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'sethome' in plugin MeckSauer-Freebuild v1.6
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_172]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_172]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_172]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at commands.SetHomeCommand.onCommand(SetHomeCommand.java:29) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    
    The thing is, that I can't understand, why it's giving me that error, because I implemented an own Error Message, if the String[] Argument is null

    Code:
        FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
        if (cmd.getName().equalsIgnoreCase("sethome")) {
            if (args.length == 1)
            {
              if (!file.exists())
              {
                Location loc = p.getLocation();
              
                double x = loc.getX();
                double y = loc.getY();
                double z = loc.getZ();
                double yaw = loc.getYaw();
                double pitch = loc.getPitch();
                String worldname = loc.getWorld().getName();
              
                cfg.set("X", Double.valueOf(x));
                cfg.set("Y", Double.valueOf(y));
                cfg.set("Z", Double.valueOf(z));
                cfg.set("Yaw", Double.valueOf(yaw));
                cfg.set("Pitch", Double.valueOf(pitch));
                cfg.set("Worldname", worldname);
                try
                {
                  cfg.save(file);
                  cfg.save(sicher);
                }
                catch (IOException e)
                {
                  e.printStackTrace();
                }
                p.sendMessage(Utils.prefix + "§aDein Homepunkt wurde gespeichert!");
              }
              else
              {
                p.sendMessage(Utils.prefix + "§cDein Homepunkt existiert bereits!");
              }
            }
            else {
              p.sendMessage(Utils.prefix + "§cNutze /sethome <Name>");
            }
    
    The Error message says, that it's caused by Class SetHomeCommand in line 29
    Code:
    File file = new File("plugins/MeckSauer-Freebuild/players/home" + p.getUniqueId(), "Home- " + args[0] + ".yml");
    
    I can't really figure out, what is wrong with that.
     
    Last edited by a moderator: Dec 11, 2018
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to plugin development.
    @Veqtive You are accessing args[0] before checking the length.
     
  3. I don't really understand.. I first chest, if the command is entered and directly after that, I check the argument length. Every Tutorial, I watched showed it like that and every other Plugin works with this type of code. Or have I misunderstood something?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Veqtive That is correct, but the call for args[0] is before both checks, because it is in the file constructor
     
  5. So if I want to fix that, i just remove the "[0]" and that's it? Or how am I able to do that?
     
  6. Offline

    timtower Administrator Administrator Moderator

    You move the constructor to within the args.length check.
     
  7. Thanks, now it's working
     
Thread Status:
Not open for further replies.

Share This Page