Solved Can't detect if args.length is 0

Discussion in 'Spigot Plugin Development' started by Solyze, Jul 18, 2020.

  1. Okay, so I have this basic invsee command, but for some reason I can't detect if args.length is 0. I've been messing around with this for ages not seeing why it wont work. I am trying to check if args length is 0, then send a usage message. I can do if (args.length != 0) { and then if I type /invsee 1 2 it will give me the usage message, but if I dont give an arg at all, it gives me an error. NO IDEA I am so tired so this may be an obvious mistake, if so, sorry

    Code (Java):
    package me.solyze.arcadia.commands;

    import org.bukkit.Bukkit;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    import net.md_5.bungee.api.ChatColor;

    import me.solyze.arcadia.Main;

    public class InvseeCommand implements CommandExecutor {

        private Plugin plugin = Main.getPlugin(Main.class);
        FileConfiguration c = plugin.getConfig();
        String prefix = c.getString("Global.Prefix");
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            Player p = (Player) sender;
            Player t = (Player) Bukkit.getPlayer(args[0]);
           
            if (p.hasPermission(c.getString("InvseeCommand.Permission"))) {
                if (args.length != 1) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix) + ChatColor.translateAlternateColorCodes('&', c.getString("InvseeCommand.Usage")));
                    return true;
                } else {
                   
                    if (t != null) {
                        if (p.isOnline()) {
                            p.openInventory(t.getInventory());
                        } else {
                            String msg = c.getString("InvseeCommand.NotOnlineOrNull");
                            msg = msg.replace("%target%", t.getName());
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix) + ChatColor.translateAlternateColorCodes('&', msg));
                        }
                    } else {
                        String msg = c.getString("InvseeCommand.NotOnlineOrNull");
                        msg = msg.replace("%target%", args[0]);
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix) + ChatColor.translateAlternateColorCodes('&', msg));
                    }
                }
               
            } else {
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix) + ChatColor.translateAlternateColorCodes('&', c.getString("InvseeCommand.NoPermission")));
            }
           
            return false;
        }

    }
    Code (Text):
    18.07 08:26:01 [Server] INFO Solyze issued server command: /invsee eew ewe
    18.07 08:26:03 [Server] INFO Solyze issued server command: /invsee eew
    18.07 08:26:04 [Server] INFO Solyze issued server command: /invsee
    18.07 08:26:04 [Server] ERROR null
    18.07 08:26:04 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'invsee' in plugin ArcadiaCore v1.0
    18.07 08:26:04 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_252]
    18.07 08:26:04 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_252]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_252]
    18.07 08:26:04 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    18.07 08:26:04 [Server] INFO at me.solyze.arcadia.commands.InvseeCommand.onCommand(InvseeCommand.java:26) ~[?:?]
    18.07 08:26:04 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    18.07 08:26:04 [Server] INFO ... 15 more
    pls help
     
  2. Just check if args is null?
    The error is a null access error. your checks arent covering a broad enough range.
     
  3. I have, but it still just gives me the same error. That's why I'm confused.
     
  4. Your error is at line 26, which appears to be where you set up the variable 't' for a player. When doing this, you are already using args[0], without first checking if there is any args, which if there is not, will cause your error, so you need to check if args is 0 before setting up that variable.
     
  5. Thank you, that fixed it :)
     
  6. Always a safe bet to check the Array before using it.