This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange behavior of length variable

 
Jagdev Singh
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During my preparation of assertions I came across the strange behavior of length variable for array.
When the code below is executed without commandLine arguments then it gives 0 as output.
But when line 2 is uncommented it gives nullPointerException, that is what I expect.
Why it is not giving any nullPointerException in the case of commandLine arguments.



Jagdev
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Command line arguments will never be null. If no argument is passed, there will simply be a zero-length array.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Null pointer exception because s is not initialized and is null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic