Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Commad line arguements puzzled

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Cmd
{
public static void main(String args[])
{
String s1=args[1];
String s2=args[2];
String s3=args[3];
String s4=args[4];
System.out.println("args[2]=" + s2);
}
}
here the command line arguments that were passed is
java Cmd 1 2 3 4
if the above it throws a null pointer exception.Please explain this case.
&
java Cmd 1 2 3 4 5
if the above it throws a null pointer exception.Please explain this case.
Please explain this.Eager to view your opinions.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by VIGNESHWAR VISWANATHAN:
...here the command line arguments that were passed is
java Cmd 1 2 3 4
if the above it throws a null pointer exception. Please explain this case...


Array indexes start at zero. So if the passed arguments are 1 2 3 4, then the length of the array is 4, but the indexes are 0, 1, 2, and 3. In particular...

args[0] is "1"
args[1] is "2"
args[2] is "3"
args[3] is "4"

Therefore, args[4] is out of bounds.
 
VIGNESHWAR VISWANATHAN
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please post the explanations
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

You will get array Index out of bounds exception because you are invoking it

java Cmd 1 2 3 4

So there is no args[4] which you are assigning to String s4.

I hope it is clear

Padma
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by VIGNESHWAR VISWANATHAN:
please post the explanations


Which detail are you not clear on?
 
Honk if you love justice! And honk twice for tiny ads!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic