PDA

View Full Version : Why it is not working "CmdLineArgsApp.class"



saneha
04-24-2011, 08:31 PM
AOA Sir,

Here is the message when run the "java CmdLineArgsApp"

D:\java>java CmdLineArgsApp
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at CmdLineArgsApp.main(CmdLineArgsApp.java:7)

What is wrong with it?

Vuhelper
04-24-2011, 08:31 PM
When while running CmdLineArgsApp you do not provide any command line arguments you get this exception.

Because in the code args[0] and args[1] are being used , if we do not provide any argument at command line, args[0] and args[1] do not have any value and exception is generated.

You can place a check to avoid this exception, you can check for number of element (i.e. check the value of args.length ) in args array if they are greater then 0 then proceed futher.