Weird Exception for this command.

Discussion in 'Plugin Development' started by Razorcane, Nov 24, 2011.

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

    Razorcane

    I have a command that takes one argument, so it's /command <argument>. I have a check to see if the args is greater than 1 or less than 1, and here is what I did.

    Code:
    if(args.length != 1) {
    return false;
    }
    Now, for some reason, it runs that code whenever I type /command <argument> <argument>, but not when I type /command. Something returns null, and I get an Exception error. I have no idea how this is caused, either.

    I've tried multiple ways of fixing this, including using conditionals and making separate if statements, and no matter what, /command always returns an Exception(but it won't tell me what).
     
  2. Paste the exception, as that shouldnt happen...
     
  3. Offline

    ItsHarry

    Show us the stack trace
     
  4. Offline

    Razorcane

    Code:
    
    2011-11-24 09:58:36 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'oguest' in plugin Origins v0.108
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:358)
    	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:757)
    	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:722)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:715)
    	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    	at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:471)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    	at org.razorcane.origins.commands.CommandOGuest.onCommand(CommandOGuest.java:23)
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    	... 12 more
    
    EDIT: Line 23 refers to this:
    Code:
    String player = args[0];
    Yes, I intentionally made it a String instead of a Player.
     
  5. Offline

    ZNickq

    You didn't type an argument :p
    For example, you wrote /command
    when you should've written /command [firstargument]

    to fix this, check for the length:
    Code:java
    1.  
    2. if(args.length==0)
    3. {
    4. cs.sendMessage("You need to put an argument!");
    5. return ;
    6. }
    7.  
     
  6. Offline

    Razorcane

    So, essentially, args.length != 1 doesn't do that?
     
  7. Offline

    ZNickq

    with args.length!=1, it won't work if the player has 2 arguments, which should still work imo ;P
     
  8. Offline

    Razorcane

    On the contrary, it only runs if the player has more than one argument, with zero arguments it returns null. I also tried that and it still returns null, the code doesn't get ran.
     
  9. Offline

    ZNickq

    ooooh, i only looked at your code now :p (i only saw the stack trace)
     
  10. You are trying to access the index 0 of the array but the check (length != 1) does not check if the array has >0 arguments. Therefor the out of bounds exception is thrown.
    Always try to do someting like
    Code:
    if (args.length > 0) {
    args[i]... // only access the array after you check that it is not empty
    }
    
    before accessing an array.
     
  11. Offline

    Razorcane

    I use a command that takes only one argument, I must also check if there is more than 1 argument.
     
  12. When u use "/command" it has zero arguments and therefor you cannot use args[0] because it does not exist.
     
  13. Offline

    halley

    Note that zero is not equal to one. if (0 != 1) { return false; } will return false. Returning false means that bukkit will show the plugin.yml help for the command.

    The command /command gives you label = "command" and args.length = 0. Accessing args[0] throws IndexOutOfBoundsException.

    The command /command blah gives you label = "command" and args.length = 1 and args[0] = "blah". Accessing args[1] throws IndexOutOfBoundsException.

    The command /command blah blah gives you label = "command" and args.length = 2 and args[0] = "blah" and args[1] = "blah". Accessing args[2] throws IndexOutOfBoundsException.

    If your command requires 1 argument, then test for that safely.

    Code:
    if (args == null || args.length < 1)
    {
        sender.sendMessage("Must give an argument.");
        return false;
    }
    if (args.length > 1)
    {
        sender.sendMessage("Ignoring extra arguments.");
        return false;
    }
    // now do the work with args[0] as the single given argument
    
     
Thread Status:
Not open for further replies.

Share This Page