I get a ArrayIndexOutOfBoundsException. The cause is this snippet:

public static void main(String[] args) {
		//command line param
		try {
			[B]if (args[0] != null) {[/B]
				if (args[0].equals("printer")) { 
					printToFile = false;
				}
			}
		} catch (Exception oob) {
			oob.printStackTrace();
		}

I don't give the program any parameters when I start it up, so args[0] is null, but that shouldn't cause this error, should it?

No, args[0] is not null. The array args has length 0. So the args[0] gives you the exception since there is no '0' element; the length of the array is 0.
The elements of the args will never be null .
Depending on the number of parameters you pass the array args will be created with those parameters. So if you pass 2 parameters, the args will have length 2. args[0], args[1] will have those values.

Edited 7 Years Ago by javaAddict: n/a

Thanks. Very useful advice!

I'm now using the following:

if (args.length > 0) {
            if (args[0].equals("printer")) {
                printToFile = false;
            }
        } else {
            printToFile = true;
        }

and it works perfectly.

This question has already been answered. Start a new discussion instead.