• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About args to main()

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I tried following code...

I u run it as java testMain
the o/p is
in main 0
Exception in thread "main" java.lang.NullPointerException
at testMain.m1(testMain.java:12)
at testMain.main(testMain.java:8)

Even if args to main are not initialised while running the program, args.length prints '0'. But if static arr a1 is not initialised and a.length is tried , it throws NullPointerException. Why so??? or i am missing something?? or args to main are automatically initialised???
Thanx in advance.
Rashmi
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi,
static arguments are also initialized. Here a[] is reference and so it's initialized to null. so when u say a.length it raises an exception.
but true, u r right about the main() method. even if u don pass anything at run time to the program it's not initialized to null but it's something like args[] = new String[0]. and so when u do args.length on main() methods parameter it says '0' as result.
if u do same thing with that static int[] a (meaning initializing like static int[] a = new int[0]) it wont give u exception but answer as '0' when u try to do a.length
HTH
Maulin
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic