views:

163

answers:

4

I am looking to do some error checking for my command line arguments

public static void main(String[] args)
{
    if(args[0] == null)
    {
        System.out.println("Proper Usage is: java program filename");
        System.exit(0);
    }
}

However, this returns an array out of bounds exception, which makes sense. I am just looking for the proper usage.

+12  A: 

The arguments can never be null. They just wont exist.

In other words, what you need to do is check the length of your arguments.

public static void main(String[] args)
{
    // Check how many arguments were passed in
    if(args.length == 0)
    {
        System.out.println("Proper Usage is: java program filename");
        System.exit(0);
    }
}
jjnguy
@Justin - Thank you
Bobby S
@Bobby, you are welcome!
jjnguy
A: 

If you don't pass any argument then even in that case args gets initialized but without any item/element. Try the following one, you will get the same effect:

 
public static void main(String[] args) throws InterruptedException {
        String [] dummy= new String [] {};
        if(dummy[0] == null)
        {
            System.out.println("Proper Usage is: java program filename");
            System.exit(0);
        }

    }

Puspendu Banerjee
This is totally incorrect. If an application is launched with no command line arguments, the `main` method will be called with an `args` array whose length is zero.
Stephen C
@Stephen An array without any element inside, has zero length. Then what's wrong about my answer?
Puspendu Banerjee
@Puspendu - what is wrong is that `dummy[0]` throws an array index out of bounds exception!! Ditto if you change the program (back) to testing `args[0]` and run it with no arguments.
Stephen C
@Stephen I have not given any solution rather I tried to describe the issue. Read "If you don't pass any argument then even in that case args gets initialized but without any item/element. Try the following one, you will get the same effect:"
Puspendu Banerjee
OK - I understand now. It would have helped if you hadn't mistyped your first sentence. As written it is unintelligible. I note that you quietly corrected it in your last comment. Sneaky.
Stephen C
Well, It's nothing "Sneaky", I have just corrected a TYPO "the" to "then"
Puspendu Banerjee
... without saying so, and at the same time implying that I hadn't read your answer properly. That's the sneaky part!
Stephen C
Please check now, I have corrected it on the main post.I appreciate your criticism will help me to become a better writer in future!
Puspendu Banerjee
+3  A: 

@jinguy's answer is correct in most circumstances. You won't ever see a null String in the argument array (or a null array) if main is called by running the application is run from the command line in the normal way.

However, if some other part of the application calls a main method, it is conceivable that it might pass a null argument or null argument array. This is clearly a highly unusual use-case, and an egregious violation of the implied contract for a main entrypoint method. Therefore, I don't think you should bother checking for null values. In the unlikely event that they do occur, I'd say that it is acceptable for the calling code to get a NullPointerException.

Stephen C
A: 

You should check for (args == null || args.length == 0). Although the null check isn't really needed, it is a good practice.

fastcodejava