954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Command line argument error

In the following example:

public class JavaExample
{
  public static void main(String[] args)
   {
   system.out.println(args[0]);
   }
}


when no parameter is passed
Exception given is java.lang.ArrayIndexOutOfBoundExpection

So my Question is that Why is that Exception is thrown , why not NullPointerException.
(I know when both expection thrown)

But in this case why the ArrayIndexOutofbound ????
Is that args is Declared and Intialized..???

de.ICeman
Newbie Poster
6 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

args is declared in your code, but it is inialised at run time with copies of the arguments that were entered on the command line. If there were no arguments it's initialised to an array of length zero (not null, just an empty array). So an attempt to reference the first element of the array [0] is an index out of bounds error.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Hmmm i didn't realize that till u told ....... Thanks from my heart.....

de.ICeman
Newbie Poster
6 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: