Command arguments pointer (ArrayIndexOutofBounds: 1)

Discussion in 'Resources' started by Jake6177, Mar 1, 2014.

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


    In reference to this post:


    Nearly all programming languages start indexing at 0. Zero. Cero. Nulo. Nul. Odo. One less than one. 0. Pound this into your head, because it is NOT going to change. Ever. Adopt it. Ingest it. Digest it. Absorb it. Sleep with it. I don't care, just get it in your silly little head.

    The reason for "ArrayIndexOutofBounds: 1" is that there is only ONE argument, yet you're looking for the SECOND place in the array. Remember, programming languages (mostly) start indexing at...what? Oh come on, you got this. That's right, ZERO.

    Zero refers to 1.
    One refers to 2.
    Two refers to 3.
    And so on goes the never-ending story.

    In short:

    To get the first argument, you need to get the zeroeth index.
    To get the second argument, you need to get the first index.
    To get the third argument, you need to get the second index.

    Tada :D
    Skyost likes this.
  2. Offline


  3. Offline


    Sorry, I don't mean to hijack the tutorial but I want to prove pictures are easier.


    For other people: As you can see, the args will simply follow as shown. In the first picture you can see which index (number between brackets) will be receiving data in order.

    So in my second example you can see arg[0] is actually the first argument, and it's taking my name, Toni as an input. My second argument (arg[1]) is the type of material. Finally, 64 would be the third argument, arg[2] (which is simply the amount of the item wanted).

    Code to show along with this would be simple as:
    1. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    2. if(sender instanceof Player) {
    3. if(command.getName().equalsIgnoreCase("give")) {
    4. // We want to make sure we receive 3 arguments: player, item, amount. Remember, arguments start at 0! So 0 - 2 is 3 arguments.
    5. if(args.length == 2) {
    6. // Assign a new player object to our first argument (arg[0]).
    7. Player player = Bukkit.getPlayer(args[0]);
    8. // Make sure the player is not invalid/offline
    9. if(player != null) {
    10. // Use the player object, get their inventory, and add an item to it!
    11. // Now as you can see, we are creating a new item stack with the constructor ItemStack(Material type, int amount)
    12. // We simply get the material from our second argument (args[1]) using the getMaterial(String input) method! We are able to do this because args return as strings!
    13. // Then we simply specify the amount by getting the integer value of our third argument (args[2])!
    14. player.getInventory().addItem(new ItemStack(Material.getMaterial(args[1]), Integer.parseInt(args[2])));
    15. // Finally after the method is called, we receive our item in our inventory!
    16. } else {
    17. sender.sendMessage("Player is offline!");
    18. }
    19. } else {
    20. sender.sendMessage("You need to specify 3 arguments! /give <player> <item> <amount>");
    21. }
    22. }
    23. return true;
    24. }
    25. return false;
    26. }

    Of course there should be more security checks; the amount in our third argument (args[2]) should be checked if it really is a integer, not assumed. But that should be a clear enough example of how it works.
    mkremins likes this.
  4. Offline


    Should also mention this applies to all arrays and lists in Java :)
    Toni likes this.
  5. Offline


    Should as well mention that SQL starts at 1 for preparedstatements -.-
  6. Offline


    valueOf is slow, use parseInt. It's for people looking at this thread and see your code, edit it so people won't mess up.

    ValueOf creates a totally new object, allocates more resources. ParseXX returns the raw value.
  7. Offline


    I'll rewrite this tomorrow is a less satirical way. I'll do it when I'm in a less satirical mood. x:
  8. Offline


    Fixed in my post. I didn't know that, well the more you learn everyday :)
  9. Offline


    Poking around here:

    valueOf just returns "new Integer(parseInt(str, 10));"

    Calling "new Integer(int value)" isn't resource intensive at all, since it only does "this.value = value;".
    Creating a new object is completely fine, in my opinion, unless you can't manage your variable references and create leaks everywhere.

    edit: What's even more is that Java has an Integer caching system for numbers -128 to 127.
  10. Offline


    In performance sensitive code, parseInt is better than valueOf. What's more, it returns a wrapper, not a primitive type. Interpreting it as an int, or
    int i Integer.valueOf("9");
    Will further increase the usage of resources.

    I never said that it was resource intensiive, I only compared the two as one being more advantegous than the other.
  11. Offline


    Well.. where's the guy who writes "your tutorial is just basic Java, this is dev.bukkit"? :p

    Anyway, simple but effective tutorial. I really hope those amount of threads won't grow. ^^
  12. Offline


    What number does the indexing start at?
  13. Offline


    I suggest going to school first and learning how to read first before trying Java :rolleyes:
Thread Status:
Not open for further replies.

Share This Page