help with args[1] == null | mob spaning plugin

Discussion in 'Plugin Development' started by enjikaka, Oct 27, 2011.

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

    enjikaka

    I'm trying to make a mobspawning plugin.
    I want to make so that if you do
    /spawnmob pig = spawns 1 pig
    /spawnmob pig 3 = spawns 3 pig

    The blue one works but now the red one :/

    This is my code:
    Code:java
    1.  
    2. if (command.equalsIgnoreCase("spawnmob")) {
    3. Block b = player.getTargetBlock(null, 200);
    4. Location loc = b.getLocation();
    5. int times = 1;
    6. String s = args[1];
    7. if (s != null) {
    8. times = Integer.valueOf(s).intValue();
    9. }
    10. if (args.length == 0) {njt.send(player, Msg.eww);}
    11. if (args[0].equalsIgnoreCase("chicken") && (Main.hasPerm(player, "mob.spawnKind", command) == true)) {
    12. for (int i = 0; i < times; i++) {
    13. b.getWorld().spawnCreature(loc, CreatureType.CHICKEN);
    14. }
    15. njt.send(player, getNode("msg.mob.youSpawned") + " " + times + " " + addS(times, args[0]));
    16. }
    17. if (args[0].equalsIgnoreCase("cow") && (Main.hasPerm(player, "mob.spawnKind", command) == true)) {
    18. for (int i = 0; i < times; i++) {
    19. b.getWorld().spawnCreature(loc, CreatureType.COW);
    20. }
    21. njt.send(player, getNode("msg.mob.youSpawned") + " " + times + " " + addS(times, args[0]));
    22. }
    23. //Way more if(args[0])'s here | I didn't paste them all ^_^
    24. }
    25.  


    What am I doing wrong?
    Shouldn't it do so that if I didn't enter a second arg it is 0?

    Thanks in advance,
    enji
     
  2. Offline

    jblaske

    This is your problem:

    Code:java
    1.  
    2. String s = args[1];
    3.  


    When you call without the second argument, its trying to find it in the string array and its not there, causing an exception, You need to test for args to have a length of at least 2, before you try to access the second value.
     
  3. Offline

    enjikaka

    I don't get it...
     
  4. Offline

    jblaske

    Perfect link @Pandemoneus

    To further elaborate what I was trying to say the first time.

    When programming you always want to make sure you can use a value before you try to use it. So make sure an object isn't null (obj != null) or that an array has the bounds you want to reference. (array.length >= value)

    Never Never assume that users will enter information correctly. The direct opposite is true, count on them always entering the wrong information. This will help you to harden your code and come out the other side with less runtime exceptions.
     
  5. Offline

    enjikaka

    I still don't get it :/ args[1] gets the string from the command I type right?
    I store that in a string named s.
    If the string is null it lets the integer times still be 1.
    If it is not null it takes the string and converts it to an integer and sets it to the inteher variable time.

    In my head, thihs should work...

    I did put a if (s != null)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  6. You can't do a check like "args[1] != null" in Java when the array is not long enough.
    As soon as the machine comes to the point "args[1]", and the args array has only a length of 1, it will throw your exception.
    This would work in several other programming lanugages, but not in Java.

    The solution is FIRST checking if args.length >= 2, THEN you can safely get args[1].
    And the best way to get an int from a String is Integer.parseInt(yourStr).
    Then you can catch a NumberFormatException when yourStr wasn't actually a number.
     
    jblaske likes this.
  7. Offline

    enjikaka

    You mean like this?
    Code:java
    1.  
    2. int times;
    3.  
    4. if (args.length >= 2) {
    5. times = 1;
    6. s = args[1];
    7. }
    8.  
    9. times = Integer.parseInt(args[1]);
    10.  


    EDIT:

    Awesome thanks you very much! :D
    It works now! :)

    I did like this:

    Code:java
    1.  
    2. int times = 1;
    3. String s;
    4. if (args.length == 2) {
    5. s = args[1];
    6. times = Integer.parseInt(s);
    7. }
    8.  
     
  8. Offline

    thehutch

    Like @Bone008 said you should also do a try/catch to catch a NumberFormatException which is if the args is not an integer then obviously you can't parse it into one so you need to catch this exception
    Code:
    try {
        times = Integer.parseInt(args[1]);
    } catch (NumberFormatException ex) {
        cs.sendMessage("Invalid number args[1] must be an integer...");
        return false;
    }
     
  9. Offline

    enjikaka

  10. Offline

    jblaske

    @enjikaka

    I would highly recommend testing for numeric rather than catching an exception. Sadly java doesn't have a build in 'isnumeric' function, so i wrote my own. Testing for numeric is faster and less resource intensive than catching exceptions.

    You are welcome to use this code

    Code:java
    1.  
    2.  
    3. public static boolean isNumeric(String value)
    4. {
    5. return value.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+");
    6. }
    7.  
    8.  


    All you need to do is test the value, a true result is a number, a false result is something that isn't a number.


    Your code would look something like this:

    Code:java
    1.  
    2. int times = 1;
    3. if (args.length == 2)
    4. {
    5. s = args[1];
    6. if(isNumeric(s))
    7. {
    8. times = Integer.parseInt(s);
    9. }
    10. else
    11. {
    12. times = 1;
    13. }
    14. }
    15.  
     
Thread Status:
Not open for further replies.

Share This Page