java netbeans commadline argument passing
I am passing a command line argument using Netbeans but I g开发者_如何学Goet an ArrayIndexOutOfBoundsException
.
My code is:
public class CmdLineArgumentPassing
{
public static void main(String args[])
{
System.out.println("Count : " + args.length);
System.out.println("i : "+args[0]);
}
}
The output is:
Count : 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Right mouse click on the project, select Properties, go to the Run page, add the command line arguments.
As your output is Count : 0
then the args
array has a length
of 0
which means no arguments are being passed.
When you try to access the first argument using arg[0]
you get an Exception as you are trying to get a member of the array which does not exist. In this case you're trying to get the first member of an empty array. Remember array indexes start at 0
and go to length - 1
.
As args
is empty it means the problem is with Netbeans passing your arguments not with your code, so my guess is that Netbeans is not configured properly.
I had the arguments set on the project properties/run/arguments but I was runing the class directly (right click over the class file -> run) , so no arguments from the project where being passed. The solution was to make right click over the project and then select the option "Run". The the arguments where passed.
Click on Final proj and run and not the main project for netbeans 6.9. U will get the answer.
精彩评论