Check if an integer is null

Discussion in 'Plugin Development' started by Spadax, Jul 31, 2017.

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


    Hello, I have a command where I use arguments to convert them to integer but I want to check whether or not they are null. When one is null, an error is sent to me:

    Integer price = Integer.valueOf(args[1]);
                        Integer second = Integer.valueOf(args[3]);
                        Integer minute = Integer.valueOf(args[4]);
                        Integer hours = Integer.valueOf(args[5]);
                        if(price.equals(null) || second.equals(null) || minute.equals(null) || hours.equals(null)) return false;
    Thanks for your help !
  2. Offline

    timtower Administrator Administrator Moderator

    @Spadax Integer can't be null.
    That will just throw a NumberFormatException when it isn't a number.
  3. Offline


    How do I avoid that?
  4. Offline

    timtower Administrator Administrator Moderator

    @Spadax You make a isInteger method, all it does is using Integer.parseInt and return true when nothing goes wrong and returns false when it throws an error.
  5. Offline


    No you are not right:

    An Integer as an Object can be null;

    Integer integ = null;

    while this (dont know the correkt word for it) cant be null;

    int inte = null; //Error
    Last edited: Aug 3, 2017
  6. Offline


    Integer is not a primitive a int is and that is why a int can't be null, so you can actually use a Integer as a object or wrapper to some sort of degree.

    You could do:
    2. Integer something = ??
    3. if something != null

    before parsing it else it will of course throw a NumberFormatException.

    edit, just because you called valueOf it invokes a parseInt thus will result in a NumberFormatException in that case just check if the string is a number.

    Just make sure that your arguments are not null.

    checking with == is fine to, however .equals does something different == checks against the reference, while .equals checks the internal parts of the object but since the object reference is already null == is more suitable.
    Last edited: Aug 3, 2017
Thread Status:
Not open for further replies.

Share This Page