user1841718
user1841718

Reputation: 187

Command-Line Arguments in NetBeans

I have a problem in NetBeans with Command-Line Arguments, when run this code it says

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 

Note I put an argument in command line for NetBeans

public class NewEmpty1
{
  public static void main(String arg[]){
   System.out.println(arg[0]);
  }
}

What is wrong ?

Upvotes: 0

Views: 2814

Answers (4)

user891193
user891193

Reputation:

public class NewMain {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int argslen=args.length;
        int argsValue[] = new int[argslen];
        for (String i:args) {
           int d = 0;
           argsValue[d]=Integer.parseInt(i);
           System.out.print(argsValue[d]+"\t"+"\n");
        }
    }
}

Upvotes: 0

Ashish Gawande
Ashish Gawande

Reputation: 11

goto Project-Property-Run here you will see the option main class arguments

now make sure you are accessing the correct main class....after this option you have button to browse the class path. select it and then select the arguments finally you should be able to run the program...cheers!

Ashish

Upvotes: 1

Arpit
Arpit

Reputation: 12797

subscript the string beyond its index is undefined.

this is your case. args[] is empty.

check this How to pass cmd line argument

Upvotes: 0

Anirudha
Anirudha

Reputation: 32797

You have not passed any arguments..

And if you have passed arguments then it may be because you are invoking another class main method in the same package


the best way would be to iterate..

for(string s:arg)
     System.out.println(s);

or

for(int i=0;i<arg.length();i++)
    System.out.println(arg[i]);

Upvotes: 0

Related Questions