Solved Checking if Something is NULL

Discussion in 'Plugin Development' started by KeybordPiano459, Dec 28, 2012.

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

    KeybordPiano459

    I'm trying to get an entitytype from a string, and check if it's null, but if it is, it still prints a stack trace in the console. Why won't this work?
    Code:java
    1. EntityType entity = EntityType.valueOf(args[0].toUpperCase());
    2. if (entity != null) {
    3. //do stuff
    4. } else {
    5. //that isnt an entitytype
    6. }
     
  2. Offline

    CorrieKay

    Read the javadocs, or the exception. Youre getting an IllegalArgumentException.

    It doesnt return null if its not found, it throws an exception if its not found. Surround it with a try catch,like this.

    Code:
    EntityType entity;
    try{
      entity = EntityType.valueOf(args[0].toUpperCase());
    } catch (IllegalArgumentAcception e){
      //entity type does not exist
      return;
    }
    //entity type exists
     
  3. Offline

    fireblast709

    Also, I cannot see you checking if the args.length > 0 (so that could cause a NPE)
     
  4. Offline

    KeybordPiano459

  5. Offline

    fireblast709

    I know, but better safe than sorry :p
     
Thread Status:
Not open for further replies.

Share This Page