Re: Command-line arg
- From: Daniel Pitts <googlegroupie@xxxxxxxxxxxxx>
- Date: Sat, 20 Oct 2007 18:14:20 -0000
On Oct 19, 4:34 pm, Wayne <nos...@xxxxxxxxxxxxxx> wrote:
Mark Space wrote:
It is theoretically possible for main() to be called with a null
argument. You may wish to check for this also or risk throwing a
NullPointer exception.
Nope. Section 12.1.4 of the JLS:
<quote>
The method main must be declared public, static, and void. It must
accept a single argument that is an array of strings. This method
can be declared as either
public static void main(String[] args)
or
public static void main(String... args)
</quote>
And Section 2.16.1 of the JVM spec:
<quote>
A Java Virtual Machine starts execution by invoking the
method main of some specified class, passing it a single
argument, which is an array of Strings. ...
</quote>
I think the only legal but not stated variation is declaring
main as:
public static void main ( String args[] )
So, if your JVM is capable of passing a null to main,
it is not a legal JVM. Invoking args.length in main
can not throw a NullPointerException in any valid JRE.
Have you ever run across a JVM that passed a NULL to main?
I've never tested any JVMs for that, but if there are
popular JVMs that do this I guess such a test would be
worthwhile.
-Wayne
Who said it was the JVM who passed it in?
SomeClass.main(new String[] {null, "oops", "npe"});
SomeClass.main(null);
.
- Follow-Ups:
- Re: Command-line arg
- From: C. Rühl
- Re: Command-line arg
- References:
- Command-line arg
- From: joe
- Re: Command-line arg
- From: Wayne
- Re: Command-line arg
- From: Mark Space
- Re: Command-line arg
- From: Wayne
- Command-line arg
- Prev by Date: Re: Setting up object arrays - loops not loopng enough times
- Next by Date: Re: Great SWT Program
- Previous by thread: Re: Command-line arg
- Next by thread: Re: Command-line arg
- Index(es):
Relevant Pages
|