1

While I was going through some tutorial one of the source code file had following to check if there were no command-line arguments :

if (null==args[0]) {
  System.err.println("Properties file not specified at command line");
  return;
}

Which for obvious reasons throws ArrayIndexOutOfBoundsException and doesn't print the message.

So,how to do this check and print the message without getting the exception being thrown?

Arjun Thakur
  • 345
  • 1
  • 5
  • 17

2 Answers2

7
if (args.length == 0) {
  System.err.println("Properties file not specified at command line");
  return;
}

When there are no arguments on the command-line, the argument array will be empty. So, you check for its length args.length==0.

Ankit
  • 6,772
  • 11
  • 48
  • 84
1
 if (args.length == 0)

Just check the length.

tckmn
  • 57,719
  • 27
  • 114
  • 156