Few errors

Discussion in 'Plugin Development' started by youngbawss22, Jan 7, 2013.

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

    youngbawss22

    Currently my code is throwing two types of errors in two different arguments.
    Main:
    Code:JAVA
    1. Player p = (Player) sender;
    2. Player tp = p.getServer().getPlayer(args[0]);
    3. int arg3 = Integer.parseInt(args[2]);
    4. if(p.hasPermission("spearmod.give")) {
    5. if(args.length == 3) {
    6. if(tp instanceof Player) {
    7. if(arg3 == (int)arg3) {
    8. if(args[1].equalsIgnoreCase("spear")) {
    9. ItemMeta im = M.addRecipeSpear();
    10. ItemStack is = new ItemStack(Material.STICK, arg3);
    11. is.setItemMeta(im);
    12. tp.getInventory().addItem(is);
    13. }
    14. else {
    15. p.sendMessage(ChatColor.RED + args[1] + ChatColor.WHITE + " is not a correct spear type!");
    16. }
    17. }
    18. else { //NumberFormatException thrown instead.
    19. p.sendMessage(args[2] + " should be an integer!");
    20. }
    21. }
    22. else {
    23. p.sendMessage("Player not found!");
    24. }
    25. }
    26. else {
    27. p.sendMessage("Incorrect Arguments!"); //ArrayIndexOutOfBoundExcpetion 0 here


    First error (number format exception): Caused by args[2] not being an integer if i type a string in args[2]. (My goal is to make it repeat the string and say "args[2] is not an int" instead).

    Second error (Array Index Out Of Bounds Exception): If i write /givespear, causes arguments to be null. Instead of the error i want it to say "Incorrect arguments!"

    Thank you!
     
  2. Offline

    fireblast709

    1st solution: use a try-catch block to catch the NumberFormatException
    2nd solution: check the args.length. args.length == 0 for no args, args.length == 1 for 1 arg, etc.
     
  3. Offline

    youngbawss22

    I was going to use a try-catch but i was not sure where to put it in regards to the if statement.
    Also, i typed only /givespear and i received the same out of bounds error.

    Current Code:
    Code:
    Player p = (Player) sender;
                    Player tp = p.getServer().getPlayer(args[0]);
                    int arg3 = Integer.parseInt(args[2]);
                    if(p.hasPermission("spearmod.give")) {
                        if(args.length == 0) {
                            p.sendMessage(ChatColor.RED  + "Too few arguments!" + ChatColor.WHITE + "Please specify the playername and spearname.");
                        }
                        else if(args.length == 1) {
                            p.sendMessage(ChatColor.RED  + "Too few arguments!" + ChatColor.WHITE + "Please specify the the spearname.");
                        }
                        else if(args.length == 2) {
                            if(tp instanceof Player) {
                                if(args[1].equalsIgnoreCase("spear")) {
                                    ItemMeta im = M.addRecipeSpear();
                                    ItemStack is = new ItemStack(Material.STICK, 1);
                                    is.setItemMeta(im);
                                    tp.getInventory().addItem(is);
                            }
                            else {
                                p.sendMessage("Player not found!");
                            }
                        }
                        else if(args.length == 3) {
                            if(tp instanceof Player) {
                                if(arg3 == (int)arg3) {
                                    if(args[1].equalsIgnoreCase("spear")) {
                                        ItemMeta im = M.addRecipeSpear();
                                        ItemStack is = new ItemStack(Material.STICK, arg3);
                                        is.setItemMeta(im);
                                        tp.getInventory().addItem(is);
                                }
                            }
                        }
                        else if(args.length > 3) {
                            p.sendMessage(ChatColor.RED + "Too many arguments!");
                        }
     
  4. Offline

    fireblast709

    the try-catch should go around the Integer.parseInt(args[x]). Btw post the code, so I can give you pointers for the ArrayIndexOutOfBoundsException
     
  5. Offline

    youngbawss22

    Just did, and what do you mean "around"? Do you mean right before it?
     
  6. Offline

    fireblast709

    Code:java
    1. int x = 0;
    2. try
    3. {
    4. x = Integer.parseInt(yourArgument);
    5. }
    6. {
    7. // this is being caught
    8. player.sendMessage("Not an integer")
    9. }
     
  7. Offline

    youngbawss22

    This code works much better, except for one problem. I do not get the error but a spear is still added to my inventory. The odd thing is i cannot remove it using /clear.
     
  8. Offline

    fireblast709

    mmh weird. Might your code's doing
     
  9. Offline

    youngbawss22

    I probably put the catch in the wrong place, i put it right under the try. And what about the other error, do you have any suggestions?
     
  10. Offline

    fireblast709

    The ArrayIndexOutOfBoundsException? probably wrongly checking the args.length in combination with the index
     
  11. Offline

    youngbawss22

    How would i check to see if they are null, since a length of 0 is null.
     
  12. Offline

    fireblast709

    Code:java
    1. if(args.length == 0)
    2. {
    3. // Its just the command
    4. // Do stuff
    5. }
    6. else if(args.length == 1)
    7. {
    etc...
     
  13. Offline

    william9518

    0 is NOT NULL!
     
  14. Offline

    youngbawss22

    I tried this, but when i enter just /givespear i get the error...
     
  15. Offline

    fireblast709

    depends what you do in the if-statement of args.length == 0
     
  16. Offline

    youngbawss22

    Code again:
    Code:JAVA
    1. if(cmd.getName().equalsIgnoreCase("givespear")) {
    2. Mech M = new Mech(plugin);
    3. if(sender instanceof Player) {
    4. Player p = (Player) sender;
    5. Player tp = p.getServer().getPlayer(args[0]);
    6. int arg3 = 0;
    7. try {
    8. arg3 = Integer.parseInt(args[2]);
    9. }
    10. p.sendMessage(ChatColor.RED + args[2] + ChatColor.WHITE + " should be an int!"); //displays message but also gives player a non-functioning spear.
    11. }
    12. if(p.hasPermission("spearmod.give")) {
    13. if(args.length == 0) {
    14. p.sendMessage(ChatColor.RED + "Too few arguments!" + ChatColor.WHITE + "Please specify the playername and spearname.");
    15. }
    16. else if(args.length == 1) {
    17. p.sendMessage(ChatColor.RED + "Too few arguments!" + ChatColor.WHITE + "Please specify the the spearname.");
    18. }
    19. else if(args.length == 2) {
    20. if(tp instanceof Player) {
    21. if(args[1].equalsIgnoreCase("spear")) {
    22. ItemMeta im = M.addRecipeSpear();
    23. ItemStack is = new ItemStack(Material.STICK, 1);
    24. is.setItemMeta(im);
    25. tp.getInventory().addItem(is);
    26. }
    27. }
    28. }
    29. else {
    30. p.sendMessage("Player not found!");
    31. }
    32. }
    33. else if(args.length == 3) {
    34. if(tp instanceof Player) {
    35. if(arg3 == (int)arg3) {
    36. if(args[1].equalsIgnoreCase("spear")) {
    37. ItemMeta im = M.addRecipeSpear();
    38. ItemStack is = new ItemStack(Material.STICK, arg3);
    39. is.setItemMeta(im);
    40. tp.getInventory().addItem(is);
    41. }
    42. }
    43. }
    44. }
    45. else if(args.length > 3) {
    46. p.sendMessage(ChatColor.RED + "Too many arguments!");
    47. }
    48. }
    49. }


    Well, then why won't args.length == 0 work? It has to be null since there are no arguments, or for some reason args.length will not work for 0.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  17. Offline

    fireblast709

    you assume the third argument is set in the try-catch, and you check it afterwards. Move that part inside the if(args.length == 3)
     
  18. Offline

    youngbawss22

    Ok, got that problem fixed. Now i need to fix the other one.

    P.S : Everything works but 0.
     
  19. Offline

    fireblast709

    whats wrong with it?
     
  20. Offline

    youngbawss22

    when i just type /givespear i get the arrayindexoutofboundserror (0 arguments).
     
  21. Offline

    fireblast709

    p.getServer().getPlayer(args[0]). You once more assumed args.length > 0 :p
     
  22. Offline

    youngbawss22

    Doesn't a string list start at 0 though?
     
  23. Offline

    fireblast709

    yes, but you try to get the args[0] before checking if args.length == 1 or more, where you try to get the target Player
     
  24. Offline

    william9518

    Fireblast... Ik you're all cool and all but
    HOW DO YOU SAVE A YamlConfiguration/Configuration?!
     
  25. Offline

    fireblast709

    saving to file is just invoking the save(File) method :3


    [offtopic] hijack much?
     
  26. Offline

    william9518

    so like file.save()?
    what is the actual object that uses the save method, do you just do save()?
     
  27. Offline

    fireblast709

    theConfig.save(File towhere)
     
  28. Offline

    william9518

    no such method!
    its that config that I asked u yesterday
     
  29. Offline

    fireblast709

  30. Offline

    william9518

Thread Status:
Not open for further replies.

Share This Page